Skip to content

Commit

Permalink
Merge pull request #500 from sneakywumpus/dev
Browse files Browse the repository at this point in the history
add missing commands to ICE, use bool where appropriate
  • Loading branch information
udo-munk authored Dec 23, 2024
2 parents 48dd396 + 44fc890 commit a5692b0
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 11 deletions.
11 changes: 6 additions & 5 deletions picosim/srcsim/disks.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ 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
*/
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];
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion picosim/srcsim/disks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
123 changes: 123 additions & 0 deletions picosim/srcsim/picosim.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/* Raspberry SDK and FatFS includes */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#if LIB_PICO_STDIO_USB || LIB_STDIO_MSC_USB
#include <tusb.h>
#endif
Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions picosim/srcsim/simcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -344,7 +344,7 @@ void config(void)
break;

case 'g':
go_flag = 1;
go_flag = true;
break;

default:
Expand Down
6 changes: 3 additions & 3 deletions picosim/srcsim/simio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions z80core/simice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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]);
Expand Down

0 comments on commit a5692b0

Please sign in to comment.