]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
merge/unify chkcpu and freq. wip
authorLeo C. <erbl259-lmu@yahoo.de>
Mon, 24 Jun 2024 09:59:56 +0000 (11:59 +0200)
committerLeo C. <erbl259-lmu@yahoo.de>
Mon, 24 Jun 2024 09:59:56 +0000 (11:59 +0200)
avr/cmd_cpu.c
z180/cpuinfo.180

index 64dd7219e71cd2b4a2b359cc58e6ac120c9cd5af..e77b9017848fc9b2dde527658d2aad80513fdb15 100644 (file)
        debug_cond(DEBUG_CPU, fmt, ##args)
 
 
+char * ulltoa (uint64_t val, char *s)
+{
+       char *p = s;
+
+       while (val >= 10) {
+               *p++ = (val % 10) + '0';
+               val = val / 10;
+       }
+       *p++ = val + '0';
+       *p = '\0';
+
+       return strrev(s);
+}
+
 /*
  * delay for <count> ms...
  */
@@ -59,24 +73,21 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles)
                /* Wait for falling edge */
                while ((EIFR & _BV(INTF6)) == 0)
                        ;
-               TCCR3B = 0b110<<CS30;   /* Count falling edges on T3 (==INT6) */
                OCR4B = TCNT4;
+               TCCR3B = 0b110<<CS30;   /* Count falling edges on T3 (==INT6) */
                TIFR4 = _BV(OCF4B);             /* clear compare match flag */
-       }
-       while (ref_ovfl < 60) {
-               ATOMIC_BLOCK(ATOMIC_FORCEON) {
+
+               while (ref_ovfl < 60) {
                        if ((TIFR4 & _BV(OCF4B)) != 0) {
                                TIFR4 = _BV(OCF4B);
-                               ref_ovfl++;
+                               ++ref_ovfl;
                        }
                        if ((TIFR3 & _BV(TOV3)) != 0) {
                                TIFR3 = _BV(TOV3);
-                               x_ovfl++;
+                               ++x_ovfl;
                        }
                }
-       }
 
-       ATOMIC_BLOCK(ATOMIC_FORCEON) {
                EIFR = _BV(INTF6);
                for (;;) {
                        if (EIFR & _BV(INTF6)) {
@@ -86,7 +97,7 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles)
                        }
                        if ((TIFR4 & _BV(OCF4B)) != 0) {
                                TIFR4 = _BV(OCF4B);
-                               ref_ovfl++;
+                               ++ref_ovfl;
                        }
                }
        }
@@ -100,12 +111,16 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles)
        uint32_t x_cnt = TCNT3 + ((uint32_t) x_ovfl << 16);
        uint64_t x_tmp = (uint64_t) 100000 * (x_cnt * cycles);
 
-       debug_cpu("TCNT3: %6u, ref_cnt: %9lu\n", TCNT3, ref_cnt);
-       debug_cpu("x_tmp: %lu %lu\n", (uint32_t) (x_tmp >> 32), (uint32_t) (x_tmp & 0xffffffff));
+//     char x_tmp_str[21];
+//
+//     debug_cpu("TCNT3: %6u, ref_cnt: %9lu\n", TCNT3, ref_cnt);
+//     ulltoa(x_tmp, x_tmp_str);
+//     debug_cpu("x_tmp: %s\n", x_tmp_str);
 
        x_tmp = (x_tmp * getenv_ulong(PSTR(ENV_FMON), 10, F_CPU) + (ref_cnt / 2)) / ref_cnt;
 
-       debug_cpu("x_tmp: %lu %lu\n", (uint32_t) (x_tmp >> 32), (uint32_t) (x_tmp & 0xffffffff));
+//     ulltoa(x_tmp, x_tmp_str);
+//     debug_cpu("x_tmp: %s\n", x_tmp_str);
 
        /* round to 5 decimal digits */
        int_fast8_t sc = 5;
@@ -126,36 +141,19 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles)
        return x_freq;
 }
 
-static const FLASH uint8_t loop_code[] = {
-/* 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[])
-{
 
 #define O_SILENT               (1<<0)
 #define O_WENV                 (1<<1)
 #define O_LOAD_LOOP     (1<<2)
 #define O_UNLOAD_LOOP   (1<<3)
 
+command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
        uint_fast8_t options = O_LOAD_LOOP | O_UNLOAD_LOOP;
-       uint_fast8_t lcycles = 19;
+       uint_fast8_t lcycles = 0;
        uint16_t timeout = 1000;
        uint8_t eimsk_save;
-
-       uint8_t mem_save[ARRAY_SIZE(loop_code)];
+       ERRNUM err = ESUCCESS;
 
        int opt;
        while ((opt = getopt(argc, argv, PSTR("swnuc:t:"))) != -1) {
@@ -185,31 +183,32 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int
        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;
+       if (z80_bus_state() & ZST_RUNNING)
+               cmd_error(CMD_RET_FAILURE, ERUNNING, NULL);
+
+       uint8_t *mem_save = NULL;
+       if (options & O_LOAD_LOOP) {
+               mem_save = (uint8_t *) malloc(cpuinfo_length);
+               if (mem_save == NULL)
+                       cmd_error(CMD_RET_FAILURE, ENOMEM, NULL);
+               z80_bus_cmd(Request);
+               z80_read_block(mem_save, 0, cpuinfo_length);
+               z80_load_mem(0, cpuinfo, &cpuinfo_sections,     cpuinfo_address,
+                                                    cpuinfo_length_of_sections);
+               z80_bus_cmd(Release);
        }
 
+       /* Save state and disable INT5/INT6 */
        ATOMIC_BLOCK(ATOMIC_FORCEON) {
-               /* Save state and disable INT5/INT6 */
                eimsk_save = EIMSK;
                EIMSK &= ~_BV(INT6);
                EIMSK &= ~_BV(INT5);
        }
-
-       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));
-       }
        EIFR = _BV(INTF5);                              /* 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... */
@@ -219,18 +218,20 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int
                }
        } while ((EIFR & _BV(INTF5)) == 0);
 
+       if (lcycles == 0) {
+               z80_bus_cmd(Request);
+               if (z80_read(3) == 0xFF)
+                       lcycles = z80_read(5);
+               z80_bus_cmd(Release);
+       }
        uint32_t cpu_freq = 0;
        if (!err)
                cpu_freq = z80_measure_phi(lcycles);
 
        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);
-       }
+
+       /* Restore INT5/INT6 */
        ATOMIC_BLOCK(ATOMIC_FORCEON) {
-               /* Restore INT5/INT6 */
                if ((eimsk_save & _BV(INT5)) != 0)
                        EIMSK |= _BV(INT5);
                if ((eimsk_save & _BV(INT6)) != 0)
@@ -239,17 +240,28 @@ command_ret_t do_cpu_freq(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int
                EIFR = _BV(INTF5);
                EIFR = _BV(INTF6);
        }
-
        Stat &= ~S_MSG_PENDING;
        Stat &= ~S_CON_PENDING;
 
+
+
+
+
+
+
+       if ((mem_save != NULL) && options & O_UNLOAD_LOOP) {
+               z80_bus_cmd(Request);
+               z80_write_block(mem_save, 0, cpuinfo_length);
+               z80_bus_cmd(Release);
+       }
+       free(mem_save);
+
        if (err)
                cmd_error(CMD_RET_FAILURE, err, NULL);
 
-       if (!(options & O_SILENT)) {
-               uint8_t sc = cpu_freq >> 28;
-               printf_P(PSTR("%lu %3u\n"), cpu_freq & 0x0fffffff, sc);
-       }
+       if (!(options & O_SILENT))
+               printf_P(PSTR("%lu\n"), cpu_freq);
+
 #if 0
        if (options & O_WENV) {
                if (setenv_ulong(PSTR(ENV_CPU_FREQ), cpu_freq)) {
@@ -275,64 +287,104 @@ 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)
 {
+       uint_fast8_t options = O_LOAD_LOOP | O_UNLOAD_LOOP;
        uint_fast8_t cputype = 0;
        ERRNUM err = ESUCCESS;
        uint8_t eimsk_save;
-       uint8_t ram_save[cpuinfo_length];
-
-       if (z80_bus_state() & ZST_RUNNING) {
-               err = ERUNNING;
-       } else {
-               z80_bus_request_or_exit();
-               z80_read_block(ram_save, 0, cpuinfo_length);
-               z80_load_mem(0, cpuinfo,
-                                       &cpuinfo_sections,
-                                       cpuinfo_address,
-                                       cpuinfo_length_of_sections);
+
+       int opt;
+       while ((opt = getopt(argc, argv, PSTR("swnu"))) != -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;
+               default: /* '?' */
+                       return CMD_RET_USAGE;
+               }
+       }
+       if (argc - optind != 0)
+               return CMD_RET_USAGE;
+
+       if (z80_bus_state() & ZST_RUNNING)
+               cmd_error(CMD_RET_FAILURE, ERUNNING, NULL);
+
+       uint8_t *mem_save = NULL;
+       if (options & O_LOAD_LOOP) {
+               mem_save = (uint8_t *) malloc(cpuinfo_length);
+               if (mem_save == NULL)
+                       cmd_error(CMD_RET_FAILURE, ENOMEM, NULL);
+               z80_bus_cmd(Request);
+               z80_read_block(mem_save, 0, cpuinfo_length);
+               z80_load_mem(0, cpuinfo, &cpuinfo_sections,     cpuinfo_address,
+                                                    cpuinfo_length_of_sections);
                z80_bus_cmd(Release);
+       }
+
+       /* Save state and disable INT5/INT6 */
+       ATOMIC_BLOCK(ATOMIC_FORCEON) {
+               eimsk_save = EIMSK;
+               EIMSK &= ~_BV(INT6);
+               EIMSK &= ~_BV(INT5);
+       }
+       EIFR = _BV(INTF5);                              /* Reset pending int */
 
-               if (argv[1] && (argv[1][0] == 'n'))
-                       goto donot;
+       z80_bus_cmd(Run);
 
-               ATOMIC_BLOCK(ATOMIC_FORCEON) {
-                       /* Save state and disable INT5/INT6 */
-                       eimsk_save = EIMSK;
-                       EIMSK &= ~_BV(INT6);
-                       EIMSK &= ~_BV(INT5);
-               }
-               EIFR = _BV(INTF5);                              /* Reset pending int */
-               z80_bus_cmd(Run);
-
-               clear_ctrlc();          /* forget any previous Control C */
-               do {
-                       /* check for ctrl-c to abort... */
-                       if (had_ctrlc() || ctrlc()) {
-                               err = EINTR;
-                               break;
-                       }
-               } while ((EIFR & _BV(INTF5)) == 0);
-               z80_bus_cmd(Reset);
-               ATOMIC_BLOCK(ATOMIC_FORCEON) {
-                       /* Restore INT5/INT6 */
-                       if ((eimsk_save & _BV(INT5)) != 0)
-                               EIMSK |= _BV(INT5);
-                       if ((eimsk_save & _BV(INT6)) != 0)
-                               EIMSK |= _BV(INT6);
-                       /* Reset pending int */
-                       EIFR = _BV(INTF5);
-                       EIFR = _BV(INTF6);
+       clear_ctrlc();          /* forget any previous Control C */
+       do {
+               /* check for ctrl-c to abort... */
+               if (had_ctrlc() || ctrlc()) {
+                       err = EINTR;
+                       break;
                }
-               Stat &= ~S_MSG_PENDING;
-               Stat &= ~S_CON_PENDING;
+       } while ((EIFR & _BV(INTF5)) == 0);
+
+
+
+
+
+
+
+
+
+
+
+       z80_bus_cmd(Reset);
+
+       /* Restore INT5/INT6 */
+       ATOMIC_BLOCK(ATOMIC_FORCEON) {
+               if ((eimsk_save & _BV(INT5)) != 0)
+                       EIMSK |= _BV(INT5);
+               if ((eimsk_save & _BV(INT6)) != 0)
+                       EIMSK |= _BV(INT6);
+               /* Reset pending int */
+               EIFR = _BV(INTF5);
+               EIFR = _BV(INTF6);
+       }
+       Stat &= ~S_MSG_PENDING;
+       Stat &= ~S_CON_PENDING;
+
+       z80_bus_cmd(Request);
+       if (z80_read(3) == 0xFF) {
+               cputype = z80_read(4);
+       }
+       z80_bus_cmd(Release);
+
+       if ((mem_save != NULL) && options & O_UNLOAD_LOOP) {
                z80_bus_cmd(Request);
-               if (z80_read(3) == 0xFF) {
-                       cputype = z80_read(4);
-               }
-               z80_write_block(ram_save, 0, cpuinfo_length);
+               z80_write_block(mem_save, 0, cpuinfo_length);
                z80_bus_cmd(Release);
        }
-
-donot:
+       free(mem_save);
 
        if (err)
                cmd_error(CMD_RET_FAILURE, err, NULL);
index 0ea3e8e17b8dfc41af7567b1722716a2b6c97868..372f00393851de4b8570dcb5989763a6a44bcb2a 100644 (file)
@@ -29,6 +29,19 @@ base         equ     0
 \r
 done:  db      0\r
 result:        db      0\r
+cycls: db      0\r
+wstates:db     0\r
+\r
+;-------------------------------------------------------------------------------\r
+cyctab:\r
+       db       0      ;Unknown CPU\r
+       db      20      ;8080\r
+       db      20      ;8085\r
+       db      21      ;Z80\r
+       db      19      ;HD64180 or higher\r
+       db      19      ;HD64180\r
+       db      19      ;Z80180\r
+       db      19      ;Z8S180, Z8L180\r
 \r
 ;-------------------------------------------------------------------------------\r
 ; Check if register C exists. D holds mask of bit to test.\r
@@ -111,9 +124,10 @@ chk_z80:
        ; Test differences in certain internal registers\r
        ; to determine the 180 variant.\r
 \r
-       ld      b,0\r
-       ld      c,icr\r
-       in      a,(c)\r
+       ld      a,(wstates)\r
+       out0    (DCNTL),a\r
+       out0    (RCR),b         ;\r
+       in0     a,(icr)\r
        cp      01FH\r
        jr      z,icr_ok\r
 \r
@@ -125,7 +139,6 @@ chk_z80:
 \r
 icr_ok:\r
        inc     e               ; HD64180\r
-       out0    (RCR),b         ;\r
        ld      c,omcr          ; Check, if CPU has OMCR register\r
        ld      d,M_IOC         ;\r
        call    chk_reg         ;\r
@@ -145,29 +158,32 @@ icr_ok:
 start:\r
        ld      sp,stack\r
        ld      hl,done\r
-       ld      (hl),0\r
+       ld      b,h\r
+       ld      (hl),b\r
        inc     hl\r
-       ld      (hl),0\r
-       push    hl\r
+       ld      (hl),b\r
        call    check\r
-       pop     hl\r
+       ld      hl,cyctab\r
+       ld      d,h\r
+       add     hl,de\r
+       ld      a,(hl)\r
+       ld      hl,cycls\r
+       ld      (hl),a\r
+       dec     hl\r
        ld      (hl),e\r
        dec     hl\r
        ld      (hl),0ffH\r
        out     (040H),a\r
+                               ;808x   Z80    Z180(0W) Z180(MaxW)\r
+loop:                          ;---------------------------------\r
+       in      a,(050h)        ;10     11       10     +3*3  19\r
+       jp      loop            ;10     10        9     +3*3  18\r
+                               ;---------------------------------\r
+                               ;20     21       19           37\r
 \r
-;      ld      a,(wstates)\r
-;      out0    (DCNTL),a\r
-                               ;Z80    Z180(0W) Z180(MaxW)\r
-loop:                          ;--------------------------\r
-       in      a,(050h)        ;11       10     +3*3  19\r
-       jp      loop            ;10        9     +3*3  18\r
-                               ;--------------------------\r
-                               ;21       19           37\r
-\r
-;      jr      loop            ;12        8     +2*3  14\r
+;      jr      loop            ;--     12        8     +2*3  14\r
 \r
-       rept    8\r
+       rept    4\r
          dw    0\r
        endm\r
 stack:\r