2 * (C) Copyright 2018 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
9 #include <util/atomic.h>
12 #include "con-utils.h"
16 #include "getopt-min.h"
19 /* hack to get Z180 loadfile into flash memory */
20 #define const const FLASH
21 #include "../z180/cpuinfo.h"
26 * delay for <count> ms...
28 static void test_delay(uint32_t count
)
30 uint32_t ts
= get_timer(0);
32 while (get_timer(ts
) <= count
);
35 static int32_t z80_measure_phi(uint8_t cycles
, bool input_clk
, uint16_t wait_ms
)
44 TCCR3B
= 0b000<<CS30
; /* stop counter */
48 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
49 /* Reset pending int */
51 /* Wait for falling edge */
52 while ((EIFR
& _BV(INTF6
)) == 0)
56 TCCR3B
= 0b110<<CS30
; /* Count falling edges on T3 (==INT6) */
57 TIFR4
= _BV(OCF4B
); /* clear compare match flag */
59 while (ref_ovfl
< 60) {
60 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
61 if ((TIFR4
& _BV(OCF4B
)) != 0) {
68 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
71 if (EIFR
& _BV(INTF6
))
73 if (TIFR4
& _BV(OCF4B
)) {
74 if (EIFR
& _BV(INTF6
))
77 if (EIFR
& _BV(INTF6
))
80 if (EIFR
& _BV(INTF6
))
87 TCCR3B
= 0b000<<CS30
; /* stop counter */
88 if ((TIFR4
& _BV(OCF4B
)) != 0) {
99 uint32_t ref_cnt
= (ref_stop
- OCR4B
) + ((uint32_t)ref_ovfl
<< 16);
101 x_freq
= TCNT3
; /* x_cnt (17 bit) */
102 if ((TIFR3
& _BV(TOV3
)) != 0)
104 uint32_t x_cnt
= x_freq
;
105 if (input_clk
) /* compute input clock */
109 x_freq
= ((uint64_t) x_freq
* F_CPU
+ (ref_cnt
/ 2))/ ref_cnt
;
111 printf_P(PSTR("ref_start: %6u, ref_stop: %6u, ref_ovfl: %4u, ref_cnt: %9lu\n"
112 " TCNT3: %6u, x_cnt: %6lu, cycles: %3u, xfreq: %9lu\n"),
113 OCR4B
, ref_stop
, ref_ovfl
, ref_cnt
,
114 TCNT3
, x_cnt
, cycles
, x_freq
);
116 /* round to 5 decimal digits */
118 while (x_freq
>= 100000UL) {
119 x_freq
= (x_freq
+ 5)/10;
130 return (int32_t) x_freq
;
134 static const FLASH
char * const FLASH cpu_strings
[] = {
145 command_ret_t
do_cpuchk(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
149 ERRNUM err
= ESUCCESS
;
150 uint8_t ram_save
[cpuinfo_length
];
152 if (z80_bus_state() & ZST_RUNNING
) {
155 z80_bus_request_or_exit();
156 z80_read_block(ram_save
, 0, cpuinfo_length
);
157 z80_load_mem(0, cpuinfo
,
160 cpuinfo_length_of_sections
);
161 z80_bus_cmd(Release
);
163 if (argv
[1] && (argv
[1][0] == 'n'))
168 clear_ctrlc(); /* forget any previous Control C */
169 while (done
!= 0xFF) {
171 /* check for ctrl-c to abort... */
172 if (had_ctrlc() || ctrlc()) {
176 z80_bus_cmd(Request
);
179 cputype
= z80_read(4);
180 z80_bus_cmd(Release
);
183 z80_bus_cmd(Request
);
184 // z80_write_block(ram_save, 0, cpuinfo_length);
185 z80_bus_cmd(Release
);
191 cmd_error(CMD_RET_FAILURE
, err
, NULL
);
194 if (cputype
>= ARRAY_SIZE(cpu_strings
))
196 printf_P(PSTR("Detected CPU: %S\n"), cpu_strings
[cputype
]);
199 return CMD_RET_SUCCESS
;
202 command_ret_t
do_cpu_test(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
205 uint32_t pulsewidth
= 10; /* ms */
211 while ((opt
= getopt(argc
, argv
, PSTR("t:"))) != -1) {
214 pulsewidth
= eval_arg(optarg
, NULL
);
217 return CMD_RET_USAGE
;
221 if ((z80_bus_state() & ZST_ACQUIRED
) != RESET
)
222 cmd_error(CMD_RET_FAILURE
, ERUNNING
, NULL
);
224 clear_ctrlc(); /* forget any previous Control C */
226 z80_bus_cmd(Request
);
227 test_delay(pulsewidth
);
228 z80_bus_cmd(Release
);
229 test_delay(pulsewidth
);
230 } while (!(had_ctrlc() || ctrlc()));
232 return CMD_RET_SUCCESS
;
235 command_ret_t
do_bus_test(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
243 while ((opt
= getopt(argc
, argv
, PSTR("t:"))) != -1) {
246 pulsewidth
= eval_arg(optarg
, NULL
);
249 return CMD_RET_USAGE
;
255 " 1: RESET 4: RUN r: Toggle /RESET\n"
256 " 2: REQUEST 5: RESTART b: Toggle /BUSREQ\n"
257 " 3: RELEASE 6: M_CYCLE\n"
266 case '1': /* bus_cmd RESET */
267 case '2': /* bus_cmd REQUEST */
268 case '3': /* bus_cmd RELEASE */
269 case '4': /* bus_cmd RUN */
270 case '5': /* bus_cmd RESTART */
271 case '6': /* bus_cmd M_CYCLE */
272 z80_bus_cmd(ch
- '1' + Reset
);
274 case 'r': /* Toggle RESET */
277 case 'b': /* Toggle BUSREQ */
282 uint32_t cycles
= z80_get_busreq_cycles();
283 printf_P(PSTR("\rState: %.2x, cycles: %lu, time: %luus "),
284 z80_bus_state(), cycles
, (uint32_t) (cycles
* 1000000LL / F_CPU
));
286 } while (ch
!= 0x03);
289 return CMD_RET_SUCCESS
;
292 command_ret_t
do_busack_test(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
295 if ((z80_bus_state() & ZST_ACQUIRED
) != RESET
)
296 cmd_error(CMD_RET_FAILURE
, ERUNNING
, NULL
);
298 z80_bus_cmd(Request
);
299 uint32_t result
= z80_get_busreq_cycles();
301 z80_bus_cmd(Release
);
306 pinconf
= gpio_config_get(pin
);
307 if (pinconf
== OUTPUT_TIMER
) {
308 div
= gpio_clockdiv_get(pin
);
313 printf_P(PSTR("cycles: %lu, time: %luus\n"), result
, (uint32_t) (result
* 1000000LL / F_CPU
));
315 return CMD_RET_SUCCESS
;
318 command_ret_t
do_cpu_freq(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
321 #define O_SILENT (1<<0)
322 #define O_WENV (1<<1)
323 #define O_LOAD_LOOP (1<<2)
324 #define O_UNLOAD_LOOP (1<<3)
326 uint_fast8_t options
= O_LOAD_LOOP
| O_UNLOAD_LOOP
;
327 uint8_t lcycles
= 18;
328 uint16_t timeout
= 1000;
332 uint8_t mem_save
[cpuinfo_length
];
338 while ((opt
= getopt(argc
, argv
, PSTR("swnuc:t:"))) != -1) {
347 options
&= ~O_LOAD_LOOP
;
350 options
&= ~O_UNLOAD_LOOP
;
353 lcycles
= eval_arg(optarg
, NULL
);
356 timeout
= eval_arg(optarg
, NULL
);
359 return CMD_RET_USAGE
;
362 if (argc
- optind
!= 0)
363 return CMD_RET_USAGE
;
365 if (z80_bus_state() & ZST_RUNNING
) {
366 if (!(options
& O_SILENT
))
367 printf_P(PSTR("Frequency measuring failed. CPU allready running!\n"));
368 return CMD_RET_FAILURE
;
371 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
372 /* Save state and disable INT6 */
375 /* Save state and set INT6 for falling edge */
377 EICRB
= (eicrb_save
& ~(0b11 << ISC60
)) | (0b10 << ISC60
);
380 z80_bus_cmd(Request
);
381 if (options
& O_LOAD_LOOP
) {
382 z80_read_block(mem_save
, 0, cpuinfo_length
);
383 z80_load_mem(0, cpuinfo
,
386 cpuinfo_length_of_sections
);
388 Stat
&= ~S_IO_0X40
; /* Reset pending int */
389 z80_bus_cmd(Release
);
392 clear_ctrlc(); /* forget any previous Control C */
395 /* Wait for falling edge */
397 /* check for ctrl-c to abort... */
398 if (had_ctrlc() || ctrlc()) {
402 } while ((Stat
& S_IO_0X40
) == 0);
406 cpu_freq
= z80_measure_phi(lcycles
, false, timeout
);
409 if (options
& O_UNLOAD_LOOP
) {
410 z80_bus_cmd(Request
);
411 z80_write_block(mem_save
, 0, cpuinfo_length
);
412 z80_bus_cmd(Release
);
414 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
417 EICRB
= (EICRB
& ~(0b11 << ISC60
)) | (eicrb_save
& (0b11 << ISC60
));
418 if ((eimsk_save
& _BV(INT6
)) != 0)
420 /* Reset pending int */
425 cmd_error(CMD_RET_FAILURE
, err
, NULL
);
427 if (!(options
& O_SILENT
)) {
428 printf_P(PSTR("%ld%S\n"), cpu_freq
, cpu_freq
< 0 ? PSTR("") : PSTR("Hz"));
429 // if (cpu_freq != 0)
431 // printf_P(PSTR("No CPU clock or input frequency to low!\n"));
434 if (options
& O_WENV
) {
435 if (setenv_ulong(PSTR(ENV_CPU_FREQ
), cpu_freq
)) {
436 if (!(options
& O_SILENT
))
437 printf_P(PSTR("'SETENV (%S, %lu)' failed!\n"), PSTR(ENV_CPU_FREQ
), cpu_freq
);
438 return CMD_RET_FAILURE
;
442 return CMD_RET_SUCCESS
;
447 * command table for fat subcommands
450 cmd_tbl_t cmd_tbl_cpu
[] = {
452 chkcpu
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_cpuchk
,
457 buscmd
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_bus_test
,
462 test
, CONFIG_SYS_MAXARGS
, 1, do_cpu_test
,
463 "Do bus request/release cycles",
467 busack
, 2, 1, do_busack_test
,
468 "Get time from /Reset high to /BUSACK low",
472 freq
, CONFIG_SYS_MAXARGS
, 1, do_cpu_freq
,
473 "Measure cpu frequency",
474 "[-qwn] [-c loopcycles] [-t timeout]\n"
476 // " -w Write result to environment variable '"ENV_CPU_FREQ"'"
480 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
481 "Print sub command description/usage",
483 " - print brief description of all sub commands\n"
484 "fat help command ...\n"
485 " - print detailed usage of sub cmd 'command'"
488 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
489 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
491 #ifdef CONFIG_SYS_LONGHELP
493 #endif /* CONFIG_SYS_LONGHELP */
495 #ifdef CONFIG_AUTO_COMPLETE
499 /* Mark end of table */
500 CMD_TBL_END(cmd_tbl_cpu
)
504 command_ret_t
do_cpu(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
506 //puts_P(PSTR("Huch?"));
508 return CMD_RET_USAGE
;
512 #if 0 /* Z180 Single Step Functions */
514 * Z180 Single Step Functions
524 #define DDR_STEP DDRG
527 #define DDR_WAIT DDRG
528 /* All three signals are on the same Port (PortG) */
529 #define PORT_SS PORTG
533 static bool ss_available
;
535 int single_step_setup(void)
537 ss_available
= false;
540 if (z80_bus_state() & ZST_RUNNING
||
541 !(z80_bus_cmd(Request
) & ZST_ACQUIRED
))
545 /* STEP, RUN output, WAIT input */
547 PORT_SS
|= _BV(RUN
) | _BV(STEP
);
548 DDR_SS
|= _BV(RUN
) | _BV(STEP
);
549 DDR_SS
&= ~_BV(WAIT
);
551 /* RUN high, MREQ pulse --> WAIT should be low */
554 if ((PIN_SS
& _BV(WAIT
)) == 0) {
556 /* RUN high, STEP pulse --> WAIT should be high */
559 if ((PIN_SS
& _BV(WAIT
)) != 0) {
561 /* RUN high, MREQ pulse --> WAIT should be low */
563 if ((PIN_SS
& _BV(WAIT
)) == 0) {
565 /* RUN low --> WAIT should be high */
567 if ((PIN_SS
& _BV(WAIT
)) != 0) {
569 /* RUN low, STEP pulse --> WAIT should be high */
572 if ((PIN_SS
& _BV(WAIT
)) != 0) {
574 /* all tests passed */
583 DDR_SS
&= ~(_BV(STEP
) | _BV(RUN
));
584 PORT_SS
|= _BV(RUN
) | _BV(STEP
);
587 return ss_available
? 0 : -1;