From 1e5609bf0dfb390b0d62651cbd979e0ee21184fe Mon Sep 17 00:00:00 2001 From: Leo C Date: Thu, 6 Sep 2018 15:39:49 +0200 Subject: [PATCH] new command: memsize. new function z80_memsize_detect(). --- avr/cmd_loadihex.c | 39 +++++++++------------------------------ avr/cmd_mem.c | 17 ++++++++++++++--- avr/command_tbl.c | 5 +++++ avr/z80-if.c | 33 +++++++++++++++++++++++++++++++++ include/cmd_mem.h | 26 +++++++++++++------------- include/z80-if.h | 1 + 6 files changed, 75 insertions(+), 46 deletions(-) diff --git a/avr/cmd_loadihex.c b/avr/cmd_loadihex.c index d55a63a..a7c9238 100644 --- a/avr/cmd_loadihex.c +++ b/avr/cmd_loadihex.c @@ -12,34 +12,6 @@ #include "debug.h" -static uint32_t detect_ramsize(void) -{ - uint32_t addr; - uint8_t save_addr, save_0; - const uint8_t PATTERN_1 = 0x55; - const uint8_t PATTERN_2 = ~PATTERN_1; - - if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) { - my_puts_P(PSTR("Bus timeout\n")); - return 0; - } - - save_0 = z80_read(0); - z80_write(0, PATTERN_1); - - for (addr=1; addr < CONFIG_SYS_RAMSIZE_MAX; addr <<= 1) { - save_addr = z80_read(addr); - z80_write(addr, PATTERN_2); - if (z80_read(0) != PATTERN_1 || z80_read(addr) != PATTERN_2) - break; - z80_write(addr, save_addr); - } - z80_write(0, save_0); - z80_bus_cmd(Release); - - return addr; -} - typedef enum { IHX_OK, IHX_BROKEN, @@ -149,15 +121,22 @@ command_ret_t do_loadihex(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int { long offset = 0; uint32_t base_address = 0; - uint32_t address_max = detect_ramsize(); uint32_t address_high = 0; - uint32_t address_low = address_max; + uint32_t address_max; + uint32_t address_low; bool firstrec = true; ihex_t rec; if (argc > 1) offset = strtol(argv[1], NULL, 16); + int32_t ram = z80_memsize_detect(); + if (ram < 0) + cmd_error(CMD_RET_FAILURE, (ERRNUM) -ram, NULL); + + address_max = ram; + address_low = address_max; + my_puts_P(PSTR("Waiting for Intel Hex Records...\n")); while (ihex_get_record(&rec) > 0 && diff --git a/avr/cmd_mem.c b/avr/cmd_mem.c index 7a8781c..d641410 100644 --- a/avr/cmd_mem.c +++ b/avr/cmd_mem.c @@ -13,11 +13,9 @@ * Copied from FADS ROM, Dan Malek (dmalek@jlc.net) */ -#include "common.h" -#include +#include "cmd_mem.h" #include -#include "command.h" #include "cli_readline.h" #include "print-utils.h" #include "con-utils.h" @@ -55,6 +53,19 @@ static ERRNUM z180_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) /*--------------------------------------------------------------------------*/ +command_ret_t do_mem_size(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) +{ + int32_t ramsize = z80_memsize_detect(); + + if (ramsize < 0) + cmd_error(CMD_RET_FAILURE, (ERRNUM) -ramsize, PSTR("Couldn't access RAM")); + + printf_P(PSTR("Detected RAM: Start %.5lx, End: %.5lx, Size: %.5lx (%ld dec)\n"), + 0l, ramsize ? ramsize-1 : 0l, ramsize, ramsize); + + return CMD_RET_SUCCESS; +} + /* Memory Display * * Syntax: diff --git a/avr/command_tbl.c b/avr/command_tbl.c index 232d718..5d185ba 100644 --- a/avr/command_tbl.c +++ b/avr/command_tbl.c @@ -74,6 +74,11 @@ CMD_TBL_ITEM( "" ), #endif +CMD_TBL_ITEM( + msize, 1, 1, do_mem_size, + "Detect memory size", + "" +), CMD_TBL_ITEM( mstep, 2, 1, do_busreq_pulse, "execute one M cycle", diff --git a/avr/z80-if.c b/avr/z80-if.c index 6183849..6d83172 100644 --- a/avr/z80-if.c +++ b/avr/z80-if.c @@ -463,6 +463,39 @@ void z80_setaddress(uint32_t addr) PIN_ADB = (((addr >> 16) << ADB_SHIFT) ^ P_ADB) & MASK(ADB_WIDTH) << ADB_SHIFT; } +int32_t z80_memsize_detect(void) +{ + const uint8_t PATTERN_1 = 0x55; + const uint8_t PATTERN_2 = ~PATTERN_1; + uint32_t addr; + + if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) + return -EBUSTO; + + uint8_t ram_0 = z80_read(0); + uint8_t ram_1 = z80_read(1); + + z80_write(0, ram_0 ^ 0xff); + z80_write(1, ram_1); + if ((z80_read(0) ^ ram_0) != 0xff) { + addr = 0; + } else { + z80_write(0, PATTERN_1); + for (addr=1; addr < CONFIG_SYS_RAMSIZE_MAX; addr <<= 1) { + uint8_t ram_i = z80_read(addr); + z80_write(addr, PATTERN_2); + if (z80_read(0) != PATTERN_1 || z80_read(addr) != PATTERN_2) + break; + z80_write(addr, ram_i); + } + } + + z80_write(0, ram_0); + z80_bus_cmd(Release); + + return addr; +} + void z80_write(uint32_t addr, uint8_t data) { z80_setaddress(addr); diff --git a/include/cmd_mem.h b/include/cmd_mem.h index b019d27..4b370cc 100644 --- a/include/cmd_mem.h +++ b/include/cmd_mem.h @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014,2018 Leo C. * * SPDX-License-Identifier: GPL-2.0 */ @@ -9,21 +9,21 @@ #include "command.h" - -extern command_ret_t do_mem_md(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_mm(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_nm(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_mw(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_cp(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_cmp(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_base(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_loop(cmd_tbl_t *, uint_fast8_t, int, char * const []); -extern command_ret_t do_mem_loopw(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_size(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_md(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_mm(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_nm(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_mw(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_cp(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_cmp(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_base(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_loop(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_loopw(cmd_tbl_t *, uint_fast8_t, int, char * const []); #ifdef CONFIG_CMD_MEMTEST -extern command_ret_t do_mem_mtest(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_mtest(cmd_tbl_t *, uint_fast8_t, int, char * const []); #endif #ifdef CONFIG_MX_CYCLIC -extern command_ret_t do_mem_mdc(cmd_tbl_t *, uint_fast8_t, int, char * const []); +command_ret_t do_mem_mdc(cmd_tbl_t *, uint_fast8_t, int, char * const []); #endif /* CONFIG_MX_CYCLIC */ #endif /* CMD_MEM_H */ diff --git a/include/z80-if.h b/include/z80-if.h index d5a5f56..0ad14f5 100644 --- a/include/z80-if.h +++ b/include/z80-if.h @@ -37,6 +37,7 @@ int z80_stat_reset(void); int z80_stat_halt(void); +int32_t z80_memsize_detect(void); void z80_write(uint32_t addr, uint8_t data); uint8_t z80_read(uint32_t addr); void z80_memset(uint32_t addr, uint8_t data, uint32_t length); -- 2.39.2