Commit MetaInfo

Revisionb67edc8d079b26b9f2d6d060b3771254939c3173 (tree)
Time2019-01-24 20:13:19
Authoringlorion <homemicro@ingl...>
Commiteringlorion

Log Message

allow emulator to work without cartridge

Change Summary

Incremental Difference

diff -r 824a35172ebe -r b67edc8d079b emulator/hm1000.c
--- a/emulator/hm1000.c Thu Jan 24 02:54:27 2019 -0800
+++ b/emulator/hm1000.c Thu Jan 24 03:13:19 2019 -0800
@@ -1006,7 +1006,6 @@
10061006
10071007 int main(int argc, char *argv[]) {
10081008 hm1k_state state;
1009- uint8_t *cartridge;
10101009 uint8_t ram[RAM_SIZE];
10111010 uint8_t rom[ROM_SIZE];
10121011 bool redraw;
@@ -1025,23 +1024,32 @@
10251024 fread(rom, 1, ROM_SIZE, f);
10261025 fclose(f);
10271026 }
1028- cartridge = malloc(0x20000);
1029- memset(cartridge, 0xff, 0x20000);
1030- {
1031- FILE *f = fopen("cartridge.bin", "rb");
1032- fread(cartridge, 1, 0x20000, f);
1033- fclose(f);
1034- }
10351027 init_6502(&state, ram);
10361028 state.rom = rom;
1037- state.cartridge = cartridge;
1038- state.cartridge_size = 0x20000;
10391029 state.io_read[SERIR - IO_BASE] = read_serir;
10401030 state.io_write[SERCR - IO_BASE] = write_sercr;
10411031 state.kbdrow = 0;
10421032 memset(state.keyboard, 0xff, sizeof(state.keyboard));
10431033 state.io_read[KBDCOL - IO_BASE] = read_kbdcol;
10441034 state.io_write[KBDROW - IO_BASE] = write_kbdrow;
1035+ do {
1036+ FILE *f = fopen("cartridge.bin", "rb");
1037+ if (!f) break;
1038+ fseek(f, 0, SEEK_END);
1039+ state.cartridge_size = ftell(f);
1040+ rewind(f);
1041+ state.cartridge = malloc(state.cartridge_size);
1042+ if (!state.cartridge) {
1043+ fprintf(stderr,
1044+ "failed to allocate memory (%lu bytes) for cartridge\n",
1045+ (unsigned long) state.cartridge_size);
1046+ perror("malloc");
1047+ break;
1048+ }
1049+ memset(state.cartridge, 0xff, state.cartridge_size);
1050+ fread(state.cartridge, 1, state.cartridge_size, f);
1051+ fclose(f);
1052+ } while (0);
10451053 reset(&state);
10461054
10471055 redraw = true;
Show on old repository browser