-static const FLASH uint8_t loop_code[] = {
- /* 0000 */ 0x01,0x36,0x00, /* ld bc,00*256+RCR */
- /* 0003 */ 0xAF, /* xor a */
- /* 0004 */ 0xED,0x79, /* out (c),a */
- /* 0006 */ 0xD3,0x40, /* out (040H),a */
- /* */ /* ;Z80 Z180(0W) Z180(MaxW) */
- /* 0008 */ /* loop: ;-------------------------- */
- /* 0008 */ 0xDB,0x50 /* in a,(050h) ;11 10 +3*3 19 */
- /* 000A */ 0xC3,0x08,0x00 /* jp loop ;10 9 +3*3 18 */
- /* ;-------------------------- */
- /* ;21 19 37 */
-}
-
-command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
-{
-
-#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;
-
- uint8_t mem_save[ARRAY_SIZE(loop_code)];
-
- int opt;
- while ((opt = getopt(argc, argv, PSTR("swnuc:t:"))) != -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;
- case 'c':
- lcycles = eval_arg(optarg, NULL);
- break;
- case 't':
- 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 (!(options & O_SILENT))
- printf_P(PSTR("Frequency measuring failed. CPU allready running!\n"));
- return CMD_RET_FAILURE;
- }
-
-
- z80_bus_cmd(Request);
- if (options & O_LOAD_LOOP) {
- z80_read_block(mem_save, 0, ARRAY_SIZE(loop_code));
- z80_write_block_P(loop_code, 0, ARRAY_SIZE(loop_code));
- }
- Stat &= ~S_IO_0X40; /* Reset pending int */
- z80_bus_cmd(Release);
- z80_bus_cmd(Run);
-
- 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);
- if (options & O_UNLOAD_LOOP) {
- z80_bus_cmd(Request);
- z80_write_block(mem_save, 0, ARRAY_SIZE(loop_code));
- z80_bus_cmd(Release);
- }
- if (err)
- cmd_error(CMD_RET_FAILURE, err, NULL);
-
- 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 (!(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;
-}
-