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 */
208 while ((opt
= getopt(argc
, argv
, PSTR("t:"))) != -1) {
211 pulsewidth
= eval_arg(optarg
, NULL
);
214 return CMD_RET_USAGE
;
218 if ((z80_bus_state() & ZST_ACQUIRED
) != RESET
)
219 cmd_error(CMD_RET_FAILURE
, ERUNNING
, NULL
);
221 clear_ctrlc(); /* forget any previous Control C */
223 z80_bus_cmd(Request
);
224 test_delay(pulsewidth
);
225 z80_bus_cmd(Release
);
226 test_delay(pulsewidth
);
227 } while (!(had_ctrlc() || ctrlc()));
229 return CMD_RET_SUCCESS
;
232 command_ret_t
do_bus_test(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
238 while ((opt
= getopt(argc
, argv
, PSTR("t:"))) != -1) {
241 pulsewidth
= eval_arg(optarg
, NULL
);
244 return CMD_RET_USAGE
;
250 " 1: RESET 4: RUN r: Toggle /RESET\n"
251 " 2: REQUEST 5: RESTART b: Toggle /BUSREQ\n"
252 " 3: RELEASE 6: M_CYCLE\n"
261 case '1': /* bus_cmd RESET */
262 case '2': /* bus_cmd REQUEST */
263 case '3': /* bus_cmd RELEASE */
264 case '4': /* bus_cmd RUN */
265 case '5': /* bus_cmd RESTART */
266 case '6': /* bus_cmd M_CYCLE */
267 z80_bus_cmd(ch
- '1' + Reset
);
269 case 'r': /* Toggle RESET */
272 case 'b': /* Toggle BUSREQ */
277 uint32_t cycles
= z80_get_busreq_cycles();
278 printf_P(PSTR("\rState: %.2x, cycles: %lu, time: %luus "),
279 z80_bus_state(), cycles
, (uint32_t) (cycles
* 1000000LL / F_CPU
));
281 } while (ch
!= 0x03);
284 return CMD_RET_SUCCESS
;
287 command_ret_t
do_busack_test(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
290 if ((z80_bus_state() & ZST_ACQUIRED
) != RESET
)
291 cmd_error(CMD_RET_FAILURE
, ERUNNING
, NULL
);
293 z80_bus_cmd(Request
);
294 uint32_t result
= z80_get_busreq_cycles();
296 z80_bus_cmd(Release
);
301 pinconf
= gpio_config_get(pin
);
302 if (pinconf
== OUTPUT_TIMER
) {
303 div
= gpio_clockdiv_get(pin
);
308 printf_P(PSTR("cycles: %lu, time: %luus\n"), result
, (uint32_t) (result
* 1000000LL / F_CPU
));
310 return CMD_RET_SUCCESS
;
313 command_ret_t
do_cpu_freq(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
316 #define O_SILENT (1<<0)
317 #define O_WENV (1<<1)
318 #define O_LOAD_LOOP (1<<2)
319 #define O_UNLOAD_LOOP (1<<3)
321 uint_fast8_t options
= O_LOAD_LOOP
| O_UNLOAD_LOOP
;
322 uint8_t lcycles
= 18;
323 uint16_t timeout
= 1000;
327 uint8_t mem_save
[cpuinfo_length
];
330 while ((opt
= getopt(argc
, argv
, PSTR("swnuc:t:"))) != -1) {
339 options
&= ~O_LOAD_LOOP
;
342 options
&= ~O_UNLOAD_LOOP
;
345 lcycles
= eval_arg(optarg
, NULL
);
348 timeout
= eval_arg(optarg
, NULL
);
351 return CMD_RET_USAGE
;
354 if (argc
- optind
!= 0)
355 return CMD_RET_USAGE
;
357 if (z80_bus_state() & ZST_RUNNING
) {
358 if (!(options
& O_SILENT
))
359 printf_P(PSTR("Frequency measuring failed. CPU allready running!\n"));
360 return CMD_RET_FAILURE
;
363 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
364 /* Save state and disable INT6 */
367 /* Save state and set INT6 for falling edge */
369 EICRB
= (eicrb_save
& ~(0b11 << ISC60
)) | (0b10 << ISC60
);
372 z80_bus_cmd(Request
);
373 if (options
& O_LOAD_LOOP
) {
374 z80_read_block(mem_save
, 0, cpuinfo_length
);
375 z80_load_mem(0, cpuinfo
,
378 cpuinfo_length_of_sections
);
380 Stat
&= ~S_IO_0X40
; /* Reset pending int */
381 z80_bus_cmd(Release
);
384 clear_ctrlc(); /* forget any previous Control C */
387 /* Wait for falling edge */
389 /* check for ctrl-c to abort... */
390 if (had_ctrlc() || ctrlc()) {
394 } while ((Stat
& S_IO_0X40
) == 0);
398 cpu_freq
= z80_measure_phi(lcycles
, false, timeout
);
401 if (options
& O_UNLOAD_LOOP
) {
402 z80_bus_cmd(Request
);
403 z80_write_block(mem_save
, 0, cpuinfo_length
);
404 z80_bus_cmd(Release
);
406 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
409 EICRB
= (EICRB
& ~(0b11 << ISC60
)) | (eicrb_save
& (0b11 << ISC60
));
410 if ((eimsk_save
& _BV(INT6
)) != 0)
412 /* Reset pending int */
417 cmd_error(CMD_RET_FAILURE
, err
, NULL
);
419 if (!(options
& O_SILENT
)) {
420 printf_P(PSTR("%ld%S\n"), cpu_freq
, cpu_freq
< 0 ? PSTR("") : PSTR("Hz"));
421 // if (cpu_freq != 0)
423 // printf_P(PSTR("No CPU clock or input frequency to low!\n"));
426 if (options
& O_WENV
) {
427 if (setenv_ulong(PSTR(ENV_CPU_FREQ
), cpu_freq
)) {
428 if (!(options
& O_SILENT
))
429 printf_P(PSTR("'SETENV (%S, %lu)' failed!\n"), PSTR(ENV_CPU_FREQ
), cpu_freq
);
430 return CMD_RET_FAILURE
;
434 return CMD_RET_SUCCESS
;
439 * command table for fat subcommands
442 cmd_tbl_t cmd_tbl_cpu
[] = {
444 chkcpu
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_cpuchk
,
449 buscmd
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_bus_test
,
454 test
, CONFIG_SYS_MAXARGS
, 1, do_cpu_test
,
455 "Do bus request/release cycles",
459 busack
, 2, 1, do_busack_test
,
460 "Get time from /Reset high to /BUSACK low",
464 freq
, CONFIG_SYS_MAXARGS
, 1, do_cpu_freq
,
465 "Measure cpu frequency",
466 "[-qwn] [-c loopcycles] [-t timeout]\n"
468 // " -w Write result to environment variable '"ENV_CPU_FREQ"'"
472 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
473 "Print sub command description/usage",
475 " - print brief description of all sub commands\n"
476 "fat help command ...\n"
477 " - print detailed usage of sub cmd 'command'"
480 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
481 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
483 #ifdef CONFIG_SYS_LONGHELP
485 #endif /* CONFIG_SYS_LONGHELP */
487 #ifdef CONFIG_AUTO_COMPLETE
491 /* Mark end of table */
492 CMD_TBL_END(cmd_tbl_cpu
)
496 command_ret_t
do_cpu(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
498 //puts_P(PSTR("Huch?"));
500 return CMD_RET_USAGE
;
504 #if 0 /* Z180 Single Step Functions */
506 * Z180 Single Step Functions
516 #define DDR_STEP DDRG
519 #define DDR_WAIT DDRG
520 /* All three signals are on the same Port (PortG) */
521 #define PORT_SS PORTG
525 static bool ss_available
;
527 int single_step_setup(void)
529 ss_available
= false;
532 if (z80_bus_state() & ZST_RUNNING
||
533 !(z80_bus_cmd(Request
) & ZST_ACQUIRED
))
537 /* STEP, RUN output, WAIT input */
539 PORT_SS
|= _BV(RUN
) | _BV(STEP
);
540 DDR_SS
|= _BV(RUN
) | _BV(STEP
);
541 DDR_SS
&= ~_BV(WAIT
);
543 /* RUN high, MREQ pulse --> WAIT should be low */
546 if ((PIN_SS
& _BV(WAIT
)) == 0) {
548 /* RUN high, STEP pulse --> WAIT should be high */
551 if ((PIN_SS
& _BV(WAIT
)) != 0) {
553 /* RUN high, MREQ pulse --> WAIT should be low */
555 if ((PIN_SS
& _BV(WAIT
)) == 0) {
557 /* RUN low --> WAIT should be high */
559 if ((PIN_SS
& _BV(WAIT
)) != 0) {
561 /* RUN low, STEP pulse --> WAIT should be high */
564 if ((PIN_SS
& _BV(WAIT
)) != 0) {
566 /* all tests passed */
575 DDR_SS
&= ~(_BV(STEP
) | _BV(RUN
));
576 PORT_SS
|= _BV(RUN
) | _BV(STEP
);
579 return ss_available
? 0 : -1;