X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/51dd0948df5ed53b11cab4e909e19ee7cee363c5..52ef24e4020adcb773adcb0d82b1c2c385610461:/avr/cmd_cpu.c diff --git a/avr/cmd_cpu.c b/avr/cmd_cpu.c index 1b1982c..d692211 100644 --- a/avr/cmd_cpu.c +++ b/avr/cmd_cpu.c @@ -6,7 +6,7 @@ #include "cmd_cpu.h" //#include -//#include +#include #include "z80-if.h" #include "con-utils.h" @@ -22,6 +22,115 @@ #undef const +/* + * delay for ms... + */ +static void test_delay(uint32_t count) +{ + uint32_t ts = get_timer(0); + + while (get_timer(ts) <= count); +} + +static int32_t z80_measure_phi(uint8_t cycles, bool input_clk, uint16_t wait_ms) +{ + uint16_t ref_stop; + uint16_t ref_ovfl; + uint32_t x_freq; + + + PRR1 &= ~_BV(PRTIM3); + TCCR3A = 0; + TCCR3B = 0b000<= 100000UL) { + x_freq = (x_freq + 5)/10; + ++sc; + } + while (sc--) + x_freq *= 10; + } + + /* Stop Timer */ + TCCR3B = 0; + PRR1 |= _BV(PRTIM3); + + return (int32_t) x_freq; +} + + static const FLASH char * const FLASH cpu_strings[] = { FSTR("Unknown CPU"), FSTR("8080"), @@ -90,24 +199,11 @@ donot: return CMD_RET_SUCCESS; } -/* - * delay for ms... - */ -static void test_delay(uint32_t count) -{ - uint32_t ts = get_timer(0); - - while (get_timer(ts) < count); -} - command_ret_t do_cpu_test(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) { uint32_t pulsewidth = 10; /* ms */ - /* reset getopt() */ - optind = 0; - int opt; while ((opt = getopt(argc, argv, PSTR("t:"))) != -1) { switch (opt) { @@ -133,12 +229,96 @@ command_ret_t do_cpu_test(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int return CMD_RET_SUCCESS; } +command_ret_t do_bus_test(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) +{ + char ch; + +#if 0 + int opt; + while ((opt = getopt(argc, argv, PSTR("t:"))) != -1) { + switch (opt) { + case 't': + pulsewidth = eval_arg(optarg, NULL); + break; + default: /* '?' */ + return CMD_RET_USAGE; + } + } +#endif + + my_puts_P(PSTR( + " 1: RESET 4: RUN r: Toggle /RESET\n" + " 2: REQUEST 5: RESTART b: Toggle /BUSREQ\n" + " 3: RELEASE 6: M_CYCLE\n" + "\n" + //"Bus state: " + )); + + do { + ch = my_getchar(1); + if (ch >= 0) { + switch (ch) { + case '1': /* bus_cmd RESET */ + case '2': /* bus_cmd REQUEST */ + case '3': /* bus_cmd RELEASE */ + case '4': /* bus_cmd RUN */ + case '5': /* bus_cmd RESTART */ + case '6': /* bus_cmd M_CYCLE */ + z80_bus_cmd(ch - '1' + Reset); + break; + case 'r': /* Toggle RESET */ + z80_toggle_reset(); + break; + case 'b': /* Toggle BUSREQ */ + z80_toggle_busreq(); + break; + } + test_delay(10); + uint32_t cycles = z80_get_busreq_cycles(); + printf_P(PSTR("\rState: %.2x, cycles: %lu, time: %luus "), + z80_bus_state(), cycles, (uint32_t) (cycles * 1000000LL / F_CPU)); + } + } while (ch != 0x03); + + putchar('\n'); + return CMD_RET_SUCCESS; +} + +command_ret_t do_busack_test(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) +{ + + if ((z80_bus_state() & ZST_ACQUIRED) != RESET) + cmd_error(CMD_RET_FAILURE, ERUNNING, NULL); + + z80_bus_cmd(Request); + uint32_t result = z80_get_busreq_cycles(); + test_delay(20); + z80_bus_cmd(Release); + #if 0 -command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, int flag UNUSED, int argc, char * const argv[]) + long div; + + pinconf = gpio_config_get(pin); + if (pinconf == OUTPUT_TIMER) { + div = gpio_clockdiv_get(pin); + } +#endif + + + printf_P(PSTR("cycles: %lu, time: %luus\n"), result, (uint32_t) (result * 1000000LL / F_CPU)); + + return CMD_RET_SUCCESS; +} + +command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) { - bool silent = false; - bool write_env = false; - bool load_loop = true; + +#define O_SILENT (1<<0) +#define O_WENV (1<<1) +#define O_LOAD_LOOP (1<<2) +#define O_UNLOAD_LOOP (1<<3) + + uint_fast8_t options = O_LOAD_LOOP | O_UNLOAD_LOOP; uint8_t lcycles = 18; uint16_t timeout = 1000; @@ -146,34 +326,36 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, int flag UNUSED, int argc, ch uint8_t eimsk_save; uint8_t mem_save[cpuinfo_length]; - /* reset getopt() */ - optind = 0; - int opt; - while ((opt = getopt(argc, argv, PSTR("swnc:t:"))) != -1) { + while ((opt = getopt(argc, argv, PSTR("swnuc:t:"))) != -1) { switch (opt) { case 's': - silent = true; + options |= O_SILENT; break; case 'w': - write_env = true; + options |= O_WENV; break; case 'n': - load_loop = false; + options &= ~O_LOAD_LOOP; + break; + case 'u': + options &= ~O_UNLOAD_LOOP; break; case 'c': lcycles = eval_arg(optarg, NULL); break; case 't': - lcycles = eval_arg(optarg, NULL); + timeout = eval_arg(optarg, NULL); break; default: /* '?' */ return CMD_RET_USAGE; } } + if (argc - optind != 0) + return CMD_RET_USAGE; if (z80_bus_state() & ZST_RUNNING) { - if (!silent) + if (!(options & O_SILENT)) printf_P(PSTR("Frequency measuring failed. CPU allready running!\n")); return CMD_RET_FAILURE; } @@ -188,24 +370,39 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, int flag UNUSED, int argc, ch } z80_bus_cmd(Request); - if (load_loop) { + if (options & O_LOAD_LOOP) { z80_read_block(mem_save, 0, cpuinfo_length); z80_load_mem(0, cpuinfo, &cpuinfo_sections, cpuinfo_address, cpuinfo_length_of_sections); } - z80_bus_cmd(Run); + Stat &= ~S_IO_0X40; /* Reset pending int */ z80_bus_cmd(Release); + z80_bus_cmd(Run); - uint32_t cpu_freq = z80_measure_phi(lcycles, true, timeout); + clear_ctrlc(); /* forget any previous Control C */ + ERRNUM err = 0; + + /* Wait for falling edge */ + do { + /* check for ctrl-c to abort... */ + if (had_ctrlc() || ctrlc()) { + err = EINTR; + break; + } + } while ((Stat & S_IO_0X40) == 0); + + int32_t cpu_freq; + if (!err) + cpu_freq = z80_measure_phi(lcycles, false, timeout); z80_bus_cmd(Reset); - z80_bus_cmd(Request); - if (load_loop) + if (options & O_UNLOAD_LOOP) { + z80_bus_cmd(Request); z80_write_block(mem_save, 0, cpuinfo_length); - z80_bus_cmd(Release); - + z80_bus_cmd(Release); + } ATOMIC_BLOCK(ATOMIC_FORCEON) { /* Restore INT6 */ eicrb_save = EICRB; @@ -216,24 +413,27 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, int flag UNUSED, int argc, ch EIFR = _BV(INTF6); } - if (!silent) { - if (cpu_freq != 0) - printf_P(PSTR("%luHz\n"), cpu_freq); - else - printf_P(PSTR("No CPU clock or input frequency to low!\n")); - } + if (err) + cmd_error(CMD_RET_FAILURE, err, NULL); - if (write_env) { + if (!(options & O_SILENT)) { + printf_P(PSTR("%ld%S\n"), cpu_freq, cpu_freq < 0 ? PSTR("") : PSTR("Hz")); +// if (cpu_freq != 0) +// else +// printf_P(PSTR("No CPU clock or input frequency to low!\n")); + } +#if 0 + if (options & O_WENV) { if (setenv_ulong(PSTR(ENV_CPU_FREQ), cpu_freq)) { - if (!silent) + 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; } -#endif + /* * command table for fat subcommands @@ -245,20 +445,29 @@ CMD_TBL_ITEM( "Check CPU", "" ), +CMD_TBL_ITEM( + buscmd, CONFIG_SYS_MAXARGS, CTBL_RPT, do_bus_test, + "Bus commands", + "" +), CMD_TBL_ITEM( test, CONFIG_SYS_MAXARGS, 1, do_cpu_test, "Do bus request/release cycles", "[-t pulsewidth]" ), -#if 0 +CMD_TBL_ITEM( + busack, 2, 1, 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", - "[-swn] [-c loopcycles]\n" - " -s Supress output (silent)\n" - " -w Write result to environment variable '"ENV_CPU_FREQ"'" + "[-qwn] [-c loopcycles] [-t timeout]\n" + " -q Be quiet\n" +// " -w Write result to environment variable '"ENV_CPU_FREQ"'" ), -#endif + CMD_TBL_ITEM( help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help, "Print sub command description/usage", @@ -292,7 +501,7 @@ command_ret_t do_cpu(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc } -#if 0 +#if 0 /* Z180 Single Step Functions */ /* * Z180 Single Step Functions *