X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/2c60e1dc26e1f424d9673541aebe778601070838..24ba732a4e11bae37bd42f3c9317a5d54597b11a:/avr/cmd_cpu.c diff --git a/avr/cmd_cpu.c b/avr/cmd_cpu.c index 745c68a..fd72b21 100644 --- a/avr/cmd_cpu.c +++ b/avr/cmd_cpu.c @@ -32,10 +32,225 @@ static void test_delay(uint32_t count) while (get_timer(ts) <= count); } -static int32_t z80_measure_phi(uint8_t cycles, uint16_t wait_ms) +uint32_t z80_measure_phi(uint_fast8_t cycles) { uint16_t ref_stop; uint16_t ref_ovfl; + uint8_t x_ovfl; + uint8_t eimsk_save,eicrb_save; + uint32_t x_freq; + + + ATOMIC_BLOCK(ATOMIC_FORCEON) { + /* Save state and disable INT6 */ + eimsk_save = EIMSK; + EIMSK &= ~_BV(INT6); + /* Save state and set INT6 for falling edge */ + eicrb_save = EICRB; + EICRB = (eicrb_save & ~(0b11 << ISC60)) | (0b10 << ISC60); + } + + PRR1 &= ~_BV(PRTIM3); + TCCR3A = 0; + TCCR3B = 0b000<> 32), (uint32_t) (x_tmp & 0xffffffff)); + + x_tmp = (x_tmp * getenv_ulong(PSTR(ENV_FMON), 10, F_CPU) + (ref_cnt / 2)) / ref_cnt; + + debug("x_tmp: %lu %lu\n", (uint32_t) (x_tmp >> 32), (uint32_t) (x_tmp & 0xffffffff)); + + /* round to 5 decimal digits */ + int_fast8_t sc = 5; + while (x_tmp >= 100000) { + x_tmp = (x_tmp + 5)/10; + sc--; + } + x_freq = x_tmp; + while (sc < 0) { + x_freq *= 10; + sc++; + } + x_freq += (uint32_t) sc << 28; + + + /* Stop Timer */ + TCCR3B = 0; + PRR1 |= _BV(PRTIM3); + + return x_freq; +} + +#if 0 +float z80_measure_phi(uint_fast8_t cycles, uint16_t wait_ms) +{ + uint16_t ref_stop; + uint16_t ref_ovfl; + uint8_t x_ovfl; + uint8_t eimsk_save,eicrb_save; + float x_freq; + + + ATOMIC_BLOCK(ATOMIC_FORCEON) { + /* Save state and disable INT6 */ + eimsk_save = EIMSK; + EIMSK &= ~_BV(INT6); + /* Save state and set INT6 for falling edge */ + eicrb_save = EICRB; + EICRB = (eicrb_save & ~(0b11 << ISC60)) | (0b10 << ISC60); + } + + PRR1 &= ~_BV(PRTIM3); + TCCR3A = 0; + TCCR3B = 0b000<= 100000UL) { x_freq = (x_freq + 5)/10; ++sc; @@ -150,7 +390,7 @@ static int32_t z80_measure_phi(uint8_t cycles, uint16_t wait_ms) return (int32_t) x_freq; } - +#endif static const FLASH char * const FLASH cpu_strings[] = { FSTR("Unknown CPU"), @@ -165,8 +405,7 @@ static const FLASH char * const FLASH cpu_strings[] = { command_ret_t do_cpuchk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) { - uint8_t done = 0; - uint8_t cputype; + uint_fast8_t cputype; ERRNUM err = ESUCCESS; uint8_t ram_save[cpuinfo_length]; @@ -187,6 +426,7 @@ command_ret_t do_cpuchk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int a z80_bus_cmd(Run); clear_ctrlc(); /* forget any previous Control C */ + uint_fast8_t done = 0; while (done != 0xFF) { _delay_ms(8); /* check for ctrl-c to abort... */ @@ -211,11 +451,9 @@ donot: if (err) cmd_error(CMD_RET_FAILURE, err, NULL); - if (done == 0xFF) { - if (cputype >= ARRAY_SIZE(cpu_strings)) - cputype = 0; - printf_P(PSTR("Detected CPU: %S\n"), cpu_strings[cputype]); - } + if (cputype >= ARRAY_SIZE(cpu_strings)) + cputype = 0; + printf_P(PSTR("Detected CPU: %S\n"), cpu_strings[cputype]); return CMD_RET_SUCCESS; } @@ -332,17 +570,19 @@ command_ret_t do_busack_test(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, } static const FLASH uint8_t loop_code[] = { - /* 0000 */ 0x01,0x36,0x00, /* ld bc,00*256+RCR ; no refresh cycles */ - /* 0003 */ 0xAF, /* xor a */ - /* 0004 */ 0xED,0x79, /* out (c),a */ - /* 0006 */ 0xD3,0x40, /* out (040H),a ; ready */ - /* */ /* */ - /* */ /* ;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 */ +/* 0000 */ 0x00, /* nop */ +/* 0001 */ 0xAF, /* xor a */ +/* 0005 */ 0xD3,0x32, /* out (032h),a ;DCNTL */ +/* 0002 */ 0xD3,0x36, /* out (036h),a ;RCR */ +/* */ /* */ +/* 0006 */ 0xD3,0x40, /* out (040H),a ;Ready */ +/* */ /* */ +/* */ /* ;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[]) @@ -354,7 +594,7 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int #define O_UNLOAD_LOOP (1<<3) uint_fast8_t options = O_LOAD_LOOP | O_UNLOAD_LOOP; - uint8_t lcycles = 18; + uint_fast8_t lcycles = 19; uint16_t timeout = 1000; uint8_t mem_save[ARRAY_SIZE(loop_code)]; @@ -415,9 +655,9 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int } } while ((Stat & S_IO_0X40) == 0); - int32_t cpu_freq; + uint32_t cpu_freq; if (!err) - cpu_freq = z80_measure_phi(lcycles, timeout); + cpu_freq = z80_measure_phi(lcycles); z80_bus_cmd(Reset); if (options & O_UNLOAD_LOOP) { @@ -429,7 +669,9 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int 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")); + printf_P(PSTR("%lu %3u\n"), cpu_freq & 0x0fffffff, cpu_freq >> 28); + +// printf_P(PSTR("%f%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"));