Skip to content

Commit 4cc0756

Browse files
committed
parent 8f7bd6c
author Bradon Kanyid <bradon.kanyid@blueowl.xyz> 1593158857 -0700 committer Bradon Kanyid <bradon.kanyid@blueowl.xyz> 1593218619 -0700 Embed multimenu into firmware, as fallback if no menu file is found at /multimenu.bin, use embedded menu built into firmware
1 parent 8f7bd6c commit 4cc0756

5 files changed

Lines changed: 66 additions & 20 deletions

File tree

code/veccart/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ modules.order
5555
Module.symvers
5656
Mkfile.old
5757
dkms.conf
58+
59+
# generated filed for multicart menu pulled from other directory (don't want it checked in here)
60+
multicart.ld

code/veccart/Makefile

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ CC = $(PREFIX)-gcc
5656
LD = $(PREFIX)-gcc
5757
OBJCOPY = $(PREFIX)-objcopy
5858
OBJDUMP = $(PREFIX)-objdump
59+
5960
# Uncomment this line if you want to use the installed (not local) library.
6061
#TOOLCHAIN_DIR = `dirname \`which $(CC)\``/../$(PREFIX)
6162
TOOLCHAIN_DIR=../libopencm3
@@ -96,6 +97,10 @@ docker-build: Dockerfile
9697
docker:
9798
docker run --rm -t -v `pwd`:/build/veccart -u `id -u ${USER}`:`id -g ${USER}` stm32-build make images $(HW_VER_ARGS)
9899

100+
bash:
101+
docker run --rm -it -v `pwd`:/build/veccart -u `id -u ${USER}`:`id -g ${USER}` stm32-build bash
102+
103+
99104
images: $(BINARY).images
100105
flash: $(BINARY).flash
101106

@@ -117,13 +122,21 @@ rom.o: rom.inc rom.c rom.h
117122
%.list: %.elf
118123
$(VERBOSE)$(OBJDUMP) -S $(*).elf > $(*).list
119124

120-
%.elf: $(OBJS) $(LDSCRIPT)
125+
%.elf: $(OBJS) $(LDSCRIPT) multicart.ld
121126
$(LD) -o $(*).elf $(OBJS) $(LDFLAGS)
122127
$(PREFIX)-size $(*).elf
123128

124129
%.o: %.c Makefile
125130
$(CC) $(CFLAGS) -o $@ -c $<
126131

132+
multicart.bin:
133+
make -C ../multicart/ docker-build
134+
make -C ../multicart/
135+
cp ../multicart/multicart.bin .
136+
137+
multicart.ld: multicart.bin
138+
cat $< | hexdump -v -e '"BYTE(0x" 1/1 "%02X" ")\n"' > $@
139+
127140
clean:
128141
rm -f $(OBJS)
129142
rm -f *.elf
@@ -133,9 +146,6 @@ clean:
133146
rm -f *.list
134147
rm -rf *.d
135148

136-
#%.flash: %.hex
137-
# stm32flash -w "$(*).hex" -b 460800 -i -rts,dtr,-dtr -v -g 0x0 /dev/ttyUSB0
138-
139149
%.flash: %.bin
140150
dfu-util -a 0 -d 0483:df11 -s 0x08000000 -D $(*).bin
141151

code/veccart/main.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
// Externs
5252
extern GameFileRecord* pActiveGameData;
53+
extern char multicart_start;
54+
extern char multicart_end;
5355

5456
//Memory for the menu ROM and the running cartridge.
5557
//We keep both in memory so we can quickly exchange them when a reset has been detected.
@@ -74,6 +76,8 @@ system_options sys_opt;
7476
char* sysData = (char*)&sys_opt;
7577
uint8_t checkDevMode = 0;
7678

79+
FILINFO cart_file_info;
80+
7781
/*
7882
//Pinning:
7983
A0-A14,SWCTL - PC0-PC15
@@ -101,12 +105,19 @@ void uart_output_func(unsigned char c){
101105
extern void romemu(void);
102106

103107
// Load a ROM into cartridge memory
108+
void loadMenu() {
109+
bool use_embedded_menu = (f_stat("/multicart.bin", &cart_file_info) != FR_OK);
110+
111+
loadRomWithHighScore("/multicart.bin", false, use_embedded_menu);
112+
}
113+
104114
void loadRom(char *fn) {
105-
loadRomWithHighScore(fn, false); // by default, do not load high score mode
115+
loadRomWithHighScore(fn, false, false); // by default, do not load high score mode
106116
}
107-
void loadRomWithHighScore(char *fn, bool load_hs_mode) {
117+
118+
void loadRomWithHighScore(char *fn, bool load_hs_mode, bool use_embedded_menu) {
108119
FIL f;
109-
FRESULT fr;
120+
FRESULT fr = FR_NO_FILE; // assume no file, so we can test if we ever opened the file later
110121
UINT r = 0;
111122
int n;
112123
UINT x;
@@ -117,14 +128,20 @@ void loadRomWithHighScore(char *fn, bool load_hs_mode) {
117128
// loaded menu data
118129
n = sizeof(menuData);
119130
}
120-
fr = f_open(&f, fn, FA_READ);
121-
if (fr) {
122-
xprintf("Error opening file: %d\n", fr);
131+
if (romData == menuData && use_embedded_menu == true) {
132+
xprintf("Copying %lu bytes of menu data... ", &multicart_end - &multicart_start);
133+
memcpy(menuData, &multicart_start, &multicart_end - &multicart_start);
134+
xprintf("OK!\n");
123135
} else {
124-
xprintf("Opened file: %s\n", fn);
136+
fr = f_open(&f, fn, FA_READ);
137+
if (fr) {
138+
xprintf("Error opening file: %d\n", fr);
139+
} else {
140+
xprintf("Opened file: %s\n", fn);
141+
}
142+
f_read(&f, romData, 64*1024, &r);
143+
xprintf("Read %d bytes of rom data.\n", r);
125144
}
126-
f_read(&f, romData, 64*1024, &r);
127-
xprintf("Read %d bytes of rom data.\n", r);
128145
// It's a game and it's <= 32KB
129146
if (n > 32*1024 && r <= 32*1024) {
130147
// pad with 0x01 for Mine Storm II and Polar Rescue (and any
@@ -192,7 +209,9 @@ void loadRomWithHighScore(char *fn, bool load_hs_mode) {
192209
parmRam[0xe0] = 0x66; // High Score flag (IDLE:0x66, LOAD2VEC:0x77, SAVE2STM:0x88)
193210
}
194211
}
195-
f_close(&f);
212+
if (fr != FR_NO_FILE) {
213+
f_close(&f);
214+
}
196215
}
197216

198217
//Stream data, for the Bad Apple demo
@@ -258,7 +277,7 @@ void doChangeRom(char* basedir, int i) {
258277

259278
romData=c_and_l.cartData;
260279
xprintf("Going to read rom image %s\n", buff);
261-
loadRomWithHighScore(buff, true);
280+
loadRomWithHighScore(buff, true, false);
262281
}
263282
}
264283

@@ -409,7 +428,6 @@ void ledsOff() {
409428
}
410429

411430
static FATFS FatFs;
412-
FILINFO cart_file_info;
413431
FIL cart_file;
414432
void doRamDisk() {
415433
/**
@@ -482,7 +500,7 @@ void doRamDisk() {
482500

483501
(void) highScoreOpenFile(); // Create/Open highscore file
484502
romData=menuData; // Explicitly setting this here so we know WTF is going on in the background
485-
loadRom("/multicart.bin");
503+
loadMenu();
486504
loadListing(menuDir, &c_and_l.listing, menuIndex+1 , menuIndex+1+0x200, romData);
487505
}
488506
} else if (sys_opt.usb_dev == USB_DEV_EXIT) {
@@ -497,7 +515,7 @@ void doRamDisk() {
497515
(void) highScoreOpenFile(); // Create/Open highscore file
498516
sys_opt.usb_dev = USB_DEV_DISABLED;
499517
romData=menuData; // Explicitly setting this here so we know WTF is going on in the background
500-
loadRom("/multicart.bin");
518+
loadMenu();
501519
loadListing(menuDir, &c_and_l.listing, menuIndex+1 , menuIndex+1+0x200, romData);
502520
}
503521

@@ -720,7 +738,7 @@ int main(void) {
720738

721739
// Load the Menu
722740
romData=menuData; // Explicitly setting this here so we know WTF is going on in the background
723-
loadRom("/multicart.bin");
741+
loadMenu();
724742
loadListing(menuDir, &c_and_l.listing, menuIndex+1 , menuIndex+1+0x200, romData);
725743
sys_opt.usb_dev = USB_DEV_DISABLED;
726744

code/veccart/main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#define MAIN_H
33

44
void uart_output_func(unsigned char c);
5+
void loadMenu(void);
56
void loadRom(char *fn);
6-
void loadRomWithHighScore(char *fn, bool load_hs_mode);
7+
void loadRomWithHighScore(char *fn, bool load_hs_mode, bool use_embedded_menu);
78
void loadStreamData(int addr, int len);
89
void doUpDir(void);
910
void doChangeDir(char* dirname);

code/veccart/stm32f411rct6.ld

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ ram (rwx) : ORIGIN = 0x20000400, LENGTH = 127K
77
}
88
/* Include the common ld script. */
99
INCLUDE cortex-m-generic.ld
10+
11+
SECTIONS {
12+
.text : {
13+
14+
/* ... */
15+
16+
multicart_start = .;
17+
INCLUDE "multicart.ld" ;
18+
multicart_end = .;
19+
20+
/* ... */
21+
}
22+
}
23+

0 commit comments

Comments
 (0)