Commit MetaInfo

Revision2848176de5b1ccc7e98ecb87402af27cc966c4b1 (tree)
Time2020-04-07 16:20:23
Authoringlorion <homemicro@ingl...>
Commiteringlorion

Log Message

added convenience functions to simplify writing tests

Change Summary

Incremental Difference

diff -r ddc25bcb44b3 -r 2848176de5b1 emulator/hm1000.c
--- a/emulator/hm1000.c Wed Jan 01 00:23:52 2020 +0000
+++ b/emulator/hm1000.c Tue Apr 07 08:20:23 2020 +0100
@@ -329,6 +329,26 @@
329329 s->ticks = 0;
330330 }
331331
332+static void init_hm1000_cartridge(
333+ hm1k_state *s, uint8_t *ram,
334+ uint8_t *rom, uint8_t *cartridge, size_t cartridge_size) {
335+ init_6502(s, ram);
336+ s->rom = rom;
337+ s->io_read[SERIR - IO_BASE] = read_serir;
338+ s->io_write[SERCR - IO_BASE] = write_sercr;
339+ s->kbdrow = 0;
340+ memset(s->keyboard, 0xff, sizeof(s->keyboard));
341+ s->io_read[KBDCOL - IO_BASE] = read_kbdcol;
342+ s->io_write[KBDROW - IO_BASE] = write_kbdrow;
343+ s->cartridge = cartridge;
344+ s->cartridge_size = cartridge_size;
345+}
346+
347+static void init_hm1000(
348+ hm1k_state *s, uint8_t *ram, uint8_t *rom) {
349+ init_hm1000_cartridge(s, ram, rom, NULL, 0);
350+}
351+
332352 static void reset(hm1k_state *s) {
333353 add_ticks(s, 7);
334354 sync_time(s);
@@ -342,6 +362,27 @@
342362 s->pc = load_u16(s, 0xfffc);
343363 }
344364
365+static void randomize_reset_hm1000(
366+ hm1k_state *s, uint8_t *ram, size_t ramsize,
367+ uint8_t *rom, size_t romsize, uint16_t reset_addr)
368+{
369+ randomize(ram, ramsize);
370+ randomize(rom, romsize);
371+ init_hm1000(s, ram, rom);
372+ rom[0x1ffc] = reset_addr & 0xff;
373+ rom[0x1ffd] = (reset_addr >> 8) & 0xff;
374+ reset(s);
375+}
376+
377+static void push(hm1k_state *s, uint8_t val) {
378+ store_u8(s, 0x100 + s->s--, val);
379+}
380+
381+static void push_u16(hm1k_state *s, uint16_t val) {
382+ push(s, (val >> 8) & 0xff);
383+ push(s, val & 0xff);
384+}
385+
345386 static uint8_t set_nz(hm1k_state *s, uint8_t val) {
346387 s->p &= 0x7d;
347388 s->p |= (val & 0x80) | ((val == 0) ? FLAG_Z : 0);
@@ -618,9 +659,7 @@
618659 }
619660
620661 OP(jsr) {
621- uint16_t nextpc = s->pc + 1;
622- store_u8(s, 0x100 + s->s--, nextpc >> 8);
623- store_u8(s, 0x100 + s->s--, nextpc & 0xff);
662+ push_u16(s, s->pc + 1);
624663 s->pc = mode_abs(s);
625664 }
626665
@@ -657,11 +696,11 @@
657696 OP(ora) { s->a = set_nz(s, s->a | load_u8(s, modes_a[op & 0x1f](s))); }
658697
659698 OP(pha) {
660- store_u8(s, 0x100 + s->s--, s->a);
699+ push(s, s->a);
661700 }
662701
663702 OP(php) {
664- store_u8(s, 0x100 + s->s--, s->p);
703+ push(s, s->p);
665704 }
666705
667706 OP(pla) {
diff -r ddc25bcb44b3 -r 2848176de5b1 emulator/test_ret1.c
--- a/emulator/test_ret1.c Wed Jan 01 00:23:52 2020 +0000
+++ b/emulator/test_ret1.c Tue Apr 07 08:20:23 2020 +0100
@@ -2,15 +2,8 @@
22
33 #include "hm1000.c"
44
5-static void push(hm1k_state *s, uint8_t value) {
6- s->ram[0x100 + s->s] = value;
7- --s->s;
8-}
9-
105 static void push_return_address(hm1k_state *s, uint16_t addr) {
11- --addr;
12- push(s, addr >> 8);
13- push(s, addr & 0xff);
6+ push_u16(s, addr - 1);
147 }
158
169 int main(int argc, char *argv[]) {
@@ -18,20 +11,9 @@
1811 uint8_t ram[RAM_SIZE];
1912 uint8_t rom[ROM_SIZE];
2013
21- randomize(ram, sizeof(ram));
22- randomize(rom, sizeof(rom));
23- rom[0x1ffc] = 0;
24- rom[0x1ffd] = 0x04;
25- init_6502(&state, ram);
26- state.rom = rom;
27- state.io_read[SERIR - IO_BASE] = read_serir;
28- state.io_write[SERCR - IO_BASE] = write_sercr;
29- state.kbdrow = 0;
30- memset(state.keyboard, 0xff, sizeof(state.keyboard));
31- state.io_read[KBDCOL - IO_BASE] = read_kbdcol;
32- state.io_write[KBDROW - IO_BASE] = write_kbdrow;
33- reset(&state);
34-
14+ randomize_reset_hm1000(
15+ &state, ram, sizeof(ram),
16+ rom, sizeof(rom), 0x400);
3517 state.a = 0x00;
3618 state.s = 0xff;
3719 push_return_address(&state, 0xfffc);
Show on old repository browser