From 1f12ca6c89dbd3e4414b9d3b997736da4df69549 Mon Sep 17 00:00:00 2001 From: "Leo C." Date: Tue, 25 Jun 2024 11:30:13 +0200 Subject: [PATCH] merge/unify chkcpu and freq. --- avr/cmd_cpu.c | 218 +++++++++++++++----------------------------------- 1 file changed, 63 insertions(+), 155 deletions(-) diff --git a/avr/cmd_cpu.c b/avr/cmd_cpu.c index e77b901..6e36b2b 100644 --- a/avr/cmd_cpu.c +++ b/avr/cmd_cpu.c @@ -141,22 +141,43 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles) return x_freq; } +static const FLASH char * const FLASH cpu_strings[] = { + FSTR("Unknown"), + FSTR("8080"), + FSTR("8085"), + FSTR("Z80"), + FSTR("x180"), + FSTR("HD64180"), + FSTR("Z80180"), + FSTR("Z80S180"), +}; #define O_SILENT (1<<0) #define O_WENV (1<<1) #define O_LOAD_LOOP (1<<2) #define O_UNLOAD_LOOP (1<<3) -command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) +static const FLASH char * const FLASH opt_strings[] = { + FSTR("swnu"), /* Options for chkcpu */ + FSTR("swnuc:t:"), /* Oprions for cpufreq */ +}; + +command_ret_t do_cpu_freq_chk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) { uint_fast8_t options = O_LOAD_LOOP | O_UNLOAD_LOOP; + uint_fast8_t cputype = 0; + uint32_t cpu_freq = 0; uint_fast8_t lcycles = 0; + uint_fast8_t freq_cmd = 0; uint16_t timeout = 1000; uint8_t eimsk_save; ERRNUM err = ESUCCESS; + if (argv[0][0] == 'f') + freq_cmd = 1; + int opt; - while ((opt = getopt(argc, argv, PSTR("swnuc:t:"))) != -1) { + while ((opt = getopt(argc, argv, opt_strings[freq_cmd])) != -1) { switch (opt) { case 's': options |= O_SILENT; @@ -218,16 +239,16 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int } } while ((EIFR & _BV(INTF5)) == 0); - if (lcycles == 0) { - z80_bus_cmd(Request); - if (z80_read(3) == 0xFF) - lcycles = z80_read(5); - z80_bus_cmd(Release); + if (freq_cmd) { + if (lcycles == 0) { + z80_bus_cmd(Request); + if (z80_read(3) == 0xFF) + lcycles = z80_read(5); + z80_bus_cmd(Release); + } + if (!err) + cpu_freq = z80_measure_phi(lcycles); } - uint32_t cpu_freq = 0; - if (!err) - cpu_freq = z80_measure_phi(lcycles); - z80_bus_cmd(Reset); /* Restore INT5/INT6 */ @@ -243,11 +264,12 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int Stat &= ~S_MSG_PENDING; Stat &= ~S_CON_PENDING; - - - - - + if (freq_cmd == 0) { + z80_bus_cmd(Request); + if (z80_read(3) == 0xFF) + cputype = z80_read(4); + z80_bus_cmd(Release); + } if ((mem_save != NULL) && options & O_UNLOAD_LOOP) { z80_bus_cmd(Request); @@ -259,139 +281,25 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int if (err) cmd_error(CMD_RET_FAILURE, err, NULL); - if (!(options & O_SILENT)) - printf_P(PSTR("%lu\n"), cpu_freq); + if (freq_cmd) { + + if (!(options & O_SILENT)) + printf_P(PSTR("%lu\n"), cpu_freq); #if 0 - if (options & O_WENV) { - if (setenv_ulong(PSTR(ENV_CPU_FREQ), cpu_freq)) { - if (!(options & O_SILENT)) - printf_P(PSTR("'SETENV (%S, %lu)' failed!\n"), PSTR(ENV_CPU_FREQ), cpu_freq); - return CMD_RET_FAILURE; + if (options & O_WENV) { + if (setenv_ulong(PSTR(ENV_CPU_FREQ), cpu_freq)) { + if (!(options & O_SILENT)) + printf_P(PSTR("'SETENV (%S, %lu)' failed!\n"), PSTR(ENV_CPU_FREQ), cpu_freq); + return CMD_RET_FAILURE; + } } - } #endif - return CMD_RET_SUCCESS; -} - -static const FLASH char * const FLASH cpu_strings[] = { - FSTR("Unknown"), - FSTR("8080"), - FSTR("8085"), - FSTR("Z80"), - FSTR("x180"), - FSTR("HD64180"), - FSTR("Z80180"), - FSTR("Z80S180"), -}; - -command_ret_t do_cpuchk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) -{ - uint_fast8_t options = O_LOAD_LOOP | O_UNLOAD_LOOP; - uint_fast8_t cputype = 0; - ERRNUM err = ESUCCESS; - uint8_t eimsk_save; - - int opt; - while ((opt = getopt(argc, argv, PSTR("swnu"))) != -1) { - switch (opt) { - case 's': - options |= O_SILENT; - break; - case 'w': - options |= O_WENV; - break; - case 'n': - options &= ~O_LOAD_LOOP; - break; - case 'u': - options &= ~O_UNLOAD_LOOP; - break; - default: /* '?' */ - return CMD_RET_USAGE; - } - } - if (argc - optind != 0) - return CMD_RET_USAGE; - - if (z80_bus_state() & ZST_RUNNING) - cmd_error(CMD_RET_FAILURE, ERUNNING, NULL); - - uint8_t *mem_save = NULL; - if (options & O_LOAD_LOOP) { - mem_save = (uint8_t *) malloc(cpuinfo_length); - if (mem_save == NULL) - cmd_error(CMD_RET_FAILURE, ENOMEM, NULL); - z80_bus_cmd(Request); - z80_read_block(mem_save, 0, cpuinfo_length); - z80_load_mem(0, cpuinfo, &cpuinfo_sections, cpuinfo_address, - cpuinfo_length_of_sections); - z80_bus_cmd(Release); - } - - /* Save state and disable INT5/INT6 */ - ATOMIC_BLOCK(ATOMIC_FORCEON) { - eimsk_save = EIMSK; - EIMSK &= ~_BV(INT6); - EIMSK &= ~_BV(INT5); + } else { + if (cputype >= ARRAY_SIZE(cpu_strings)) + cputype = 0; + printf_P(PSTR("Detected CPU: %S\n"), cpu_strings[cputype]); } - EIFR = _BV(INTF5); /* Reset pending int */ - - z80_bus_cmd(Run); - - clear_ctrlc(); /* forget any previous Control C */ - do { - /* check for ctrl-c to abort... */ - if (had_ctrlc() || ctrlc()) { - err = EINTR; - break; - } - } while ((EIFR & _BV(INTF5)) == 0); - - - - - - - - - - - - z80_bus_cmd(Reset); - - /* Restore INT5/INT6 */ - ATOMIC_BLOCK(ATOMIC_FORCEON) { - if ((eimsk_save & _BV(INT5)) != 0) - EIMSK |= _BV(INT5); - if ((eimsk_save & _BV(INT6)) != 0) - EIMSK |= _BV(INT6); - /* Reset pending int */ - EIFR = _BV(INTF5); - EIFR = _BV(INTF6); - } - Stat &= ~S_MSG_PENDING; - Stat &= ~S_CON_PENDING; - - z80_bus_cmd(Request); - if (z80_read(3) == 0xFF) { - cputype = z80_read(4); - } - z80_bus_cmd(Release); - - if ((mem_save != NULL) && options & O_UNLOAD_LOOP) { - z80_bus_cmd(Request); - z80_write_block(mem_save, 0, cpuinfo_length); - z80_bus_cmd(Release); - } - free(mem_save); - - if (err) - cmd_error(CMD_RET_FAILURE, err, NULL); - - if (cputype >= ARRAY_SIZE(cpu_strings)) - cputype = 0; - printf_P(PSTR("Detected CPU: %S\n"), cpu_strings[cputype]); return CMD_RET_SUCCESS; } @@ -514,7 +422,14 @@ command_ret_t do_busack_test(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, cmd_tbl_t cmd_tbl_cpu[] = { CMD_TBL_ITEM( - chkcpu, CONFIG_SYS_MAXARGS, CTBL_RPT|CTBL_SUBCMDAUTO, do_cpuchk, + freq, CONFIG_SYS_MAXARGS, CTBL_RPT, do_cpu_freq_chk, + "Measure cpu frequency", + "[-qwn] [-c loopcycles] [-t timeout]\n" + " -q Be quiet\n" +// " -w Write result to environment variable '"ENV_CPU_FREQ"'" +), +CMD_TBL_ITEM( + chkcpu, CONFIG_SYS_MAXARGS, CTBL_RPT|CTBL_SUBCMDAUTO, do_cpu_freq_chk, "Check/Identify CPU", "" ), @@ -524,22 +439,15 @@ CMD_TBL_ITEM( "" ), CMD_TBL_ITEM( - test, CONFIG_SYS_MAXARGS, 1, do_cpu_test, + test, CONFIG_SYS_MAXARGS, CTBL_RPT, do_cpu_test, "Do bus request/release cycles", "[-t pulsewidth]" ), CMD_TBL_ITEM( - busack, 2, 1, do_busack_test, + busack, 2, CTBL_RPT, do_busack_test, "Get time from /Reset high to /BUSACK low", "" ), -CMD_TBL_ITEM( - freq, CONFIG_SYS_MAXARGS, 1, do_cpu_freq, - "Measure cpu frequency", - "[-qwn] [-c loopcycles] [-t timeout]\n" - " -q Be quiet\n" -// " -w Write result to environment variable '"ENV_CPU_FREQ"'" -), CMD_TBL_ITEM( help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help, @@ -551,7 +459,7 @@ CMD_TBL_ITEM( ), /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */ - {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help, + {FSTR("?"), CONFIG_SYS_MAXARGS, CTBL_RPT, do_help, NULL, #ifdef CONFIG_SYS_LONGHELP FSTR(""), -- 2.39.2