From 98859e261db1fb71aa2fdaab7d33013c26948ef7 Mon Sep 17 00:00:00 2001 From: Thomas Eberhardt Date: Mon, 23 Dec 2024 21:07:07 +0100 Subject: [PATCH 1/2] add missing commands to ICE, use bool where appropriate Adds 'c', 'r', and '! ls' to the ICE. save PC in simice.c:do_clock() --- picosim/srcsim/disks.c | 9 +-- picosim/srcsim/disks.h | 2 +- picosim/srcsim/picosim.c | 123 +++++++++++++++++++++++++++++++++++++++ picosim/srcsim/simcfg.c | 4 +- picosim/srcsim/simio.c | 6 +- z80core/simice.c | 3 + 6 files changed, 137 insertions(+), 10 deletions(-) diff --git a/picosim/srcsim/disks.c b/picosim/srcsim/disks.c index 7b96f64b..16fe6deb 100644 --- a/picosim/srcsim/disks.c +++ b/picosim/srcsim/disks.c @@ -142,9 +142,10 @@ void list_files(const char *dir, const char *ext) * load a file 'name' into memory * returns 1 on success, 0 on error */ -int load_file(const char *name) +bool load_file(const char *name) { - int i = 0, res; + int i = 0; + bool res; register unsigned int j; unsigned int br; char SFN[25]; @@ -170,10 +171,10 @@ int load_file(const char *name) } if (sd_res != FR_OK) { printf("f_read error: %s (%d)\n", FRESULT_str(sd_res), sd_res); - res = 0; + res = false; } else { printf("loaded file \"%s\" (%d bytes)\n", SFN, i + br); - res = 1; + res = true; } f_close(&sd_file); diff --git a/picosim/srcsim/disks.h b/picosim/srcsim/disks.h index 360c21e0..41bb1a31 100644 --- a/picosim/srcsim/disks.h +++ b/picosim/srcsim/disks.h @@ -27,7 +27,7 @@ extern char disks[NUMDISK][DISKLEN]; extern void init_disks(void), exit_disks(void); extern void list_files(const char *dir, const char *ext); -extern int load_file(const char *name); +extern bool load_file(const char *name); extern void check_disks(void); extern void mount_disk(int drive, const char *name); diff --git a/picosim/srcsim/picosim.c b/picosim/srcsim/picosim.c index b86f53a3..8975edf7 100644 --- a/picosim/srcsim/picosim.c +++ b/picosim/srcsim/picosim.c @@ -18,6 +18,7 @@ /* Raspberry SDK and FatFS includes */ #include #include +#include #if LIB_PICO_STDIO_USB || LIB_STDIO_MSC_USB #include #endif @@ -51,6 +52,11 @@ #include "disks.h" #include "rgbled.h" +#ifdef WANT_ICE +static void picosim_ice_cmd(char *cmd, WORD *wrk_addr); +static void picosim_ice_help(void); +#endif + #define BS 0x08 /* ASCII backspace */ #define DEL 0x7f /* ASCII delete */ @@ -204,6 +210,8 @@ int main(void) /* run the CPU with whatever is in memory */ #ifdef WANT_ICE + ice_cust_cmd = picosim_ice_cmd; + ice_cust_help = picosim_ice_help; ice_cmd_loop(0); #else run_cpu(); @@ -261,3 +269,118 @@ int get_cmdline(char *buf, int len) putchar('\n'); return 0; } + +#ifdef WANT_ICE + +/* + * This function is the callback for the alarm. + * The CPU emulation is stopped here. + */ +static int64_t timeout(alarm_id_t id, void *user_data) +{ + UNUSED(id); + UNUSED(user_data); + + cpu_state = ST_STOPPED; + return 0; +} + +static void picosim_ice_cmd(char *cmd, WORD *wrk_addr) +{ + char *s; + BYTE save[3]; + WORD save_PC; + Tstates_t T0; +#ifdef WANT_HB + int save_hb_flag; +#endif + + switch (tolower((unsigned char) *cmd)) { + case 'c': + /* + * Calculate the clock frequency of the emulated CPU: + * into memory locations 0000H to 0002H the following + * code will be stored: + * LOOP: JP LOOP + * It uses 10 T states for each execution. A 3 second + * alarm is set and then the CPU started. For every JP + * the T states counter is incremented by 10 and after + * the timer is down and stops the emulation, the clock + * speed of the CPU in MHz is calculated with: + * f = (T - T0) / 3000000 + */ + +#ifdef WANT_HB + save_hb_flag = hb_flag; + hb_flag = 0; +#endif + save[0] = getmem(0x0000); /* save memory locations */ + save[1] = getmem(0x0001); /* 0000H - 0002H */ + save[2] = getmem(0x0002); + putmem(0x0000, 0xc3); /* store opcode JP 0000H at address */ + putmem(0x0001, 0x00); /* 0000H */ + putmem(0x0002, 0x00); + save_PC = PC; /* save PC */ + PC = 0; /* set PC to this code */ + T0 = T; /* remember start clock counter */ + add_alarm_in_ms(3000, timeout, /* set 3 second alarm */ + NULL, true); + run_cpu(); /* start CPU */ + PC = save_PC; /* restore PC */ + putmem(0x0000, save[0]); /* restore memory locations */ + putmem(0x0001, save[1]); /* 0000H - 0002H */ + putmem(0x0002, save[2]); +#ifdef WANT_HB + hb_flag = save_hb_flag; +#endif +#ifndef EXCLUDE_Z80 + if (cpu == Z80) + s = "JP"; +#endif +#ifndef EXCLUDE_I8080 + if (cpu == I8080) + s = "JMP"; +#endif + if (cpu_error == NONE) { + printf("CPU executed %" PRIu64 " %s instructions " + "in 3 seconds\n", (T - T0) / 10, s); + printf("clock frequency = %5.2f MHz\n", + ((float) (T - T0)) / 3000000.0); + } else + puts("Interrupted by user"); + break; + + case 'r': + cmd++; + while (isspace((unsigned char) *cmd)) + cmd++; + for (s = cmd; *s; s++) + *s = toupper((unsigned char) *s); + if (load_file(cmd)) + *wrk_addr = PC = 0; + break; + + case '!': + cmd++; + while (isspace((unsigned char) *cmd)) + cmd++; + if (strcasecmp(cmd, "ls") == 0) + list_files("/CODE80", "*.BIN"); + else + puts("what??"); + break; + + default: + puts("what??"); + break; + } +} + +static void picosim_ice_help(void) +{ + puts("c measure clock frequency"); + puts("r filename read file (without .BIN) into memory"); + puts("! ls list files"); +} + +#endif diff --git a/picosim/srcsim/simcfg.c b/picosim/srcsim/simcfg.c index a67fb0b4..c340f60c 100644 --- a/picosim/srcsim/simcfg.c +++ b/picosim/srcsim/simcfg.c @@ -92,7 +92,7 @@ void config(void) const char *dext = "*.DSK"; char s[10]; unsigned int br; - int go_flag = 0; + bool go_flag = false; int i, n, menu; struct tm t = { .tm_year = 124, .tm_mon = 0, .tm_mday = 1, .tm_wday = 1, .tm_hour = 0, .tm_min = 0, .tm_sec = 0, @@ -344,7 +344,7 @@ void config(void) break; case 'g': - go_flag = 1; + go_flag = true; break; default: diff --git a/picosim/srcsim/simio.c b/picosim/srcsim/simio.c index 315b91a1..b3f1236b 100644 --- a/picosim/srcsim/simio.c +++ b/picosim/srcsim/simio.c @@ -127,17 +127,17 @@ static BYTE p000_in(void) */ static BYTE p001_in(void) { - int input_avail = 0; + bool input_avail = false; #if LIB_PICO_STDIO_UART uart_inst_t *my_uart = uart_default; if (uart_is_readable(my_uart)) - input_avail = 1; + input_avail = true; #endif #if LIB_PICO_STDIO_USB || (LIB_STDIO_MSC_USB && !STDIO_MSC_USB_DISABLE_STDIO) if (tud_cdc_connected() && tud_cdc_available()) - input_avail = 1; + input_avail = true; #endif if (input_avail) sio_last = getchar(); diff --git a/z80core/simice.c b/z80core/simice.c index 1aa82ad3..d4271f0e 100644 --- a/z80core/simice.c +++ b/z80core/simice.c @@ -1301,6 +1301,7 @@ static void do_help(void) static void do_clock(void) { BYTE save[3]; + WORD save_PC; Tstates_t T0; static struct sigaction newact; static struct itimerval tim; @@ -1317,6 +1318,7 @@ static void do_clock(void) putmem(0x0000, 0xc3); /* store opcode JP 0000H at address */ putmem(0x0001, 0x00); /* 0000H */ putmem(0x0002, 0x00); + save_PC = PC; /* save PC */ PC = 0; /* set PC to this code */ T0 = T; /* remember start clock counter */ newact.sa_handler = timeout; /* set timer interrupt handler */ @@ -1331,6 +1333,7 @@ static void do_clock(void) run_cpu(); /* start CPU */ newact.sa_handler = SIG_DFL; /* reset timer interrupt handler */ sigaction(SIGALRM, &newact, NULL); + PC = save_PC; /* restore PC */ putmem(0x0000, save[0]); /* restore memory locations */ putmem(0x0001, save[1]); /* 0000H - 0002H */ putmem(0x0002, save[2]); From 44fc890927753cbc1bae99083841219b0d0f7882 Mon Sep 17 00:00:00 2001 From: Thomas Eberhardt Date: Mon, 23 Dec 2024 21:11:17 +0100 Subject: [PATCH 2/2] forgot comment --- picosim/srcsim/disks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picosim/srcsim/disks.c b/picosim/srcsim/disks.c index 16fe6deb..1d8f0c45 100644 --- a/picosim/srcsim/disks.c +++ b/picosim/srcsim/disks.c @@ -140,7 +140,7 @@ void list_files(const char *dir, const char *ext) /* * load a file 'name' into memory - * returns 1 on success, 0 on error + * returns true on success, false on error */ bool load_file(const char *name) {