-
Notifications
You must be signed in to change notification settings - Fork 0
/
chipcon_v2.c
382 lines (341 loc) · 7.53 KB
/
chipcon_v2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright 2014, 2015, Jacques Deschênes
* This file is part of CHIPcon v2.
*
* CHIPcon is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CHIPcon is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CHIPcon v2. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* chipcon.c
*
* Created: 2014-09-22 20:05:57
* Author: Jacques Deschênes
*/
// doit-être inclus avant <util/delay.h>
#include "hardware.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <util/delay.h>
// fichiers d'entête du projet
#include "tvout.h"
#include "joystick.h"
#include "sram.h"
#include "text.h"
#include "tone.h"
#include "chipcon_vm.h"
#include "splash.h"
#if SDC_SUPPORT
#include "filesys.h"
#endif
//jeux en mémoire FLASH
#include "lem.h"
#include "car.h"
#include "worm.h"
#include "alien.h"
#include "blinky.h"
#include "field.h"
#include "piper.h"
#include "square.h"
#include "octopac.h"
#if TEST
#include "joymus.h"
#include "saverstr.h"
#include "speed.h"
#endif
extern vm_state_t vms;
void load_flash_game(const uint8_t *addr,uint16_t size){
uint16_t i;
for (i=0;i<size;i++){
sram_write_byte(CODE_BASE_ADDR+i,pgm_read_byte(&addr[i]));
}
}
#define MAX_ENTRIES (2048)
#define DISPLAY_LINES (VRES/CHAR_HEIGHT)
uint8_t display_page(int16_t page){
uint8_t fname[ENTRY_SIZE];
uint8_t i;
for (i=0;i<DISPLAY_LINES;i++){
sram_load_block(page*DISPLAY_LINES*ENTRY_SIZE+(i*ENTRY_SIZE),ENTRY_SIZE,fname);
if (strlen((char *)fname)){
put_char(' ');
print(fname);
if (i<(DISPLAY_LINES-1)) new_line();
}else{
break;
}
}
return i;
}
inline void display_cursor(uint8_t line){
put_sprite(0,line*CHAR_HEIGHT,CHAR_HEIGHT,&font_6x8[(RIGHT_ARROW-32)*CHAR_HEIGHT],FLASH_MEM);
}
// sélectionne un fichier
// à partir de la liste
// affichée à l'écran
int16_t select_file(int16_t fcount){
int16_t page, selected;
uint8_t key, pg_lines;
cls();
page=0;
selected=0;
frame_delay=2;
while (frame_delay);
pg_lines=display_page(page);
while (1){
display_cursor(selected);
key=joystick_wait_any();
//key_tone(key,6,false);
display_cursor(selected);
switch(key){
case BTN_FIRE:
joystick_wait_release();
return page*DISPLAY_LINES+selected;
break;
case BTN_LEFT: // page précédente
if (page){
page--;
selected=7;
cls();
pg_lines=display_page(page);
}else{
selected=0;
}
break;
case BTN_RIGHT: // page suivante
if ((page*DISPLAY_LINES+DISPLAY_LINES)<fcount){
page++;
selected=0;
cls();
pg_lines=display_page(page);
}else{
selected=pg_lines-1;
}
break;
case BTN_UP: // item précédent
if (selected){
selected--;
}else if (page){
page--;
selected=7;
cls();
pg_lines=display_page(page);
}
break;
case BTN_DOWN: // item suivant
if (selected<(pg_lines-1)){
selected++;
}else if ((page*DISPLAY_LINES+DISPLAY_LINES)<fcount){
page++;
selected=0;
cls();
pg_lines=display_page(page);
}
break;
default:
break;
}//switch
while (_joystick_state()!=JSTK_IMASK)key_tone(key,2,true);
}//while
}
char ext[]=".bin";
char extu[]=".BIN";
#if SDC_SUPPORT
// copie la liste des fichiers
// du répertoire racine
// dans la SRAM
int16_t sdc_files(){
uint8_t fname[ENTRY_SIZE];
int16_t entry_nbr;
struct fat_dir_entry_struct dir_entry;
/* charge les fichiers dans la SRAM */
entry_nbr=0;
fname[ENTRY_SIZE-1]=0;
while((entry_nbr<MAX_ENTRIES) && fs_read_dir(&dir_entry))
{
if (!(dir_entry.attributes & FAT_ATTRIB_DIR) && ((strstr(dir_entry.long_name,ext))||(strstr(dir_entry.long_name,extu)))){
strcpy((char*)fname,dir_entry.long_name);
sram_store_block(entry_nbr*ENTRY_SIZE,ENTRY_SIZE,fname);
entry_nbr++;
}
}
fname[0]=0;
sram_store_block(entry_nbr*ENTRY_SIZE,ENTRY_SIZE,fname);
return entry_nbr;
}
void games_on_sdcard(){
int16_t fcount, selected;
fcount=sdc_files();
if (fcount && ((selected=select_file(fcount))>-1)){
if (fs_load_file(selected)){
cls();
if (schipp()==CHIP_BAD_OPCODE){
print_vms(PSTR("CRASH! bad opcode\n"));
}
}
}
}
#endif
//nom des jeux en flash
PROGMEM const uint8_t flash_games[]=
"LEM\n" // case 0
"CAR\n" // case 1
"WORM\n" // case 2
"ALIEN\n" // case 3
"BLINKY\n" // case 4
"FIELD!\n" // case 5
"PIPER\n" // case 6
"MAGIC SQUARE\n" // case 7
"OCTO PACMAN\n" //8
#if TEST
"JOYMUS\n"
"save-restore\n"
"speed\n"
#endif
"";
// sélection et exécution
// d'un jeux qui est en
// mémoire flash
void games_in_flash(){
uint16_t i=0,j,entry=0,selected;
uint8_t c, name[32];
memset(name,0,ENTRY_SIZE);
while ((c=pgm_read_byte(&flash_games[i++]))){
j=0;
while (c && (c!='\n')){
name[j++]=c;
c=pgm_read_byte(&flash_games[i++]);
}
sram_store_block(entry*ENTRY_SIZE,ENTRY_SIZE,name);
entry++;
memset(name,0,ENTRY_SIZE);
if (!c) break;
}
sram_store_block(entry*ENTRY_SIZE,ENTRY_SIZE,name);
cls();
selected=select_file(entry);
switch(selected){
case 0:
load_flash_game((const uint8_t*)lem,LEM_SIZE);
break;
case 1:
load_flash_game((const uint8_t*)car,CAR_SIZE);
break;
case 2:
load_flash_game(worm,WORM_SIZE);
break;
case 3:
load_flash_game(alien,ALIEN_SIZE);
break;
case 4:
load_flash_game(blinky,BLINKY_SIZE);
break;
case 5:
load_flash_game(field,FIELD_SIZE);
break;
case 6:
load_flash_game(piper,PIPER_SIZE);
break;
case 7:
load_flash_game(magic_square,SQUARE_SIZE);
break;
case 8:
load_flash_game(octopac,OCTOPAC_SIZE);
break;
#if TEST
case 9:
load_flash_game(joymus,JOYMUS_SIZE);
break;
case 10:
load_flash_game(saverstr,SAVERSTR_SIZE);
break;
case 11:
load_flash_game(speed,SPEED_SIZE);
break;
#endif
default:
return;
}
cls();
if (schipp()==CHIP_BAD_OPCODE){
print_vms(PSTR("CRASH! bad opcode\n"));
};
}
PROGMEM const uint8_t credits[]=
"FAT driver\n"
"Copyright 2006-2012\n"
"Roland Riegel\n";
void splash_screen(){
uint16_t i;
for (i=0;i<VIDEO_BUFF_SIZE;i++) video_write_byte(i,pgm_read_byte((splash+i)));
frame_delay=60;
while (frame_delay){
if (_joystick_state()!=JSTK_IMASK){
tone(1000,2);
frame_delay=0;
}
}
joystick_wait_release();
cls();
}
int main(void){
uint8_t sdc_ok=0;
CLKPR= (1<<7);
CLKPR = 0 ; // désactivation du diviseur
tvout_init();
select_font(FONT_ASCII);
//initialisation SPI
SPI_DDR |= SPI_CLK+SPI_MOSI;
SPI_CR = (1<<SPE)|(1<<MSTR);
SPI_SR |= 1; // Fosc/2
// initialisation du joystick Atari 2600
_joystick_init();
// initialisation sons
tone_init();
// initialisation mémoire SRAM
sram_init();
sei();// activation des interruptions
splash_screen();
srand(TCNT1);
#if SDC_SUPPORT
//initialisation carte SD
if (sd_raw_init() && fs_mount()){
sdc_ok=fs_open_dir("/");
}else{
SPI_FAST_CLOCK();
}
#endif
#if !FW_DEBUG
text_scroller(credits,4);
#endif
while (1){
//sram_clear();
cls();
select_font(FONT_ASCII);
#if SDC_SUPPORT==1
if (sdc_ok){
games_on_sdcard();
}else{
prt_pstr(PSTR("carte SD?\n"));
prompt_btn();
games_in_flash();
}
#else
games_in_flash();
#endif
}//while(1)
return EXIT_SUCCESS;
}