summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore10
-rw-r--r--.gitmodules3
-rw-r--r--Tupfile.ini0
-rw-r--r--Tuprules.tup3
-rw-r--r--avr/Tupfile70
-rw-r--r--avr/background.c31
-rw-r--r--avr/background.h10
-rw-r--r--avr/cli.c356
-rw-r--r--avr/cli.h64
-rw-r--r--avr/cli_readline.c595
-rw-r--r--avr/cli_readline.h50
-rw-r--r--avr/cmd_boot.c145
-rw-r--r--avr/cmd_echo.c58
-rw-r--r--avr/cmd_help.c32
-rw-r--r--avr/cmd_mem.c878
-rw-r--r--avr/cmd_mem.h30
-rw-r--r--avr/command.c526
-rw-r--r--avr/command.h157
-rw-r--r--avr/command_tbl.c201
-rw-r--r--avr/common.h61
-rw-r--r--avr/con-utils.c95
-rw-r--r--avr/con-utils.h22
-rw-r--r--avr/config.h28
-rw-r--r--avr/crc.h12
-rw-r--r--avr/debug.c227
-rw-r--r--avr/debug.h34
-rw-r--r--avr/env.c833
-rw-r--r--avr/env.h10
-rw-r--r--avr/main.c212
-rw-r--r--avr/ring.h74
-rw-r--r--avr/serial.c123
-rw-r--r--avr/serial.h9
-rw-r--r--avr/timer.c87
-rw-r--r--avr/timer.h7
-rw-r--r--avr/xmalloc.c27
-rw-r--r--avr/xmalloc.h8
-rw-r--r--avr/z180-serv.c327
-rw-r--r--avr/z180-stamp-avr.c591
-rw-r--r--avr/z80-if.c672
-rw-r--r--avr/z80-if.h46
-rw-r--r--configs/debug.config1
-rw-r--r--configs/default.config0
-rw-r--r--configs/gcc.tup40
m---------libopencm30
-rw-r--r--stm32/Tupfile74
-rw-r--r--stm32/debug.h11
-rw-r--r--stm32/serial.c194
-rw-r--r--stm32/serial.h8
-rw-r--r--stm32/stm32vl-discovery.ld31
-rw-r--r--stm32/z180-stamp-stm32.c763
-rw-r--r--stm32/z80-if.c731
-rw-r--r--stm32/z80-if.h32
-rw-r--r--z180/Makefile126
-rw-r--r--z180/Tupfile52
-rw-r--r--z180/config.inc105
-rw-r--r--z180/console.18048
-rw-r--r--z180/ddtz.1806370
-rw-r--r--z180/fifoio.180128
-rw-r--r--z180/msgbuf.180129
-rw-r--r--z180/r3init.180883
-rw-r--r--z180/romend.1809
-rw-r--r--z180/ser1-i.180257
-rw-r--r--z180/z180.lib120
-rw-r--r--z180/z180reg.inc190
64 files changed, 17026 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1dc5864
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,10 @@
+#Makefiledev
+/.cproject
+/.project
+/.settings
+/.tup
+/build-debug
+/doc
+/joe
+/scratch
+
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..6bccd68
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "libopencm3"]
+ path = libopencm3
+ url = https://github.com/libopencm3/libopencm3
diff --git a/Tupfile.ini b/Tupfile.ini
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Tupfile.ini
diff --git a/Tuprules.tup b/Tuprules.tup
new file mode 100644
index 0000000..185c6bf
--- /dev/null
+++ b/Tuprules.tup
@@ -0,0 +1,3 @@
+.gitignore
+TOP = $(TUP_CWD)
+
diff --git a/avr/Tupfile b/avr/Tupfile
new file mode 100644
index 0000000..199b86c
--- /dev/null
+++ b/avr/Tupfile
@@ -0,0 +1,70 @@
+include_rules
+
+PROG = stamp-test
+SRC = main.c
+SRC += cli.c cli_readline.c command.c command_tbl.c
+SRC += cmd_help.c cmd_echo.c cmd_mem.c cmd_boot.c
+SRC += env.c xmalloc.c
+SRC += timer.c con-utils.c serial.c
+SRC += background.c z180-serv.c z80-if.c
+
+SRC_Z = ../z180/hdrom.c
+
+#TARGETS = $(PROG).elf
+
+MCU_TARGET = atmega1281
+F_CPU = 18432000UL
+DEFS = -DF_CPU=$(F_CPU) -DBAUD=115200UL
+
+#INCLUDES += ../z180
+
+###############################################################################
+
+TOOLCHAINDIR =
+TOOLCHAIN = avr
+
+CC = $(TOOLCHAIN)-gcc
+LD = $(TOOLCHAIN)-gcc
+AR = $(TOOLCHAIN)-ar
+AS = $(TOOLCHAIN)-as
+OBJCOPY = $(TOOLCHAIN)-objcopy
+OBJDUMP = $(TOOLCHAIN)-objdump
+SIZE = $(TOOLCHAIN)-size
+GDB = $(TOOLCHAIN)-gdb
+
+###############################################################################
+
+ifdef DEBUG
+SRC += debug.c
+DEFS += -DDEBUG=2
+endif
+
+CFLAGS = -g -Os
+CFLAGS += -mmcu=$(MCU_TARGET)
+CFLAGS += -std=gnu99
+CFLAGS += -Wall -Wextra
+CFLAGS += -Wredundant-decls
+#CFLAGS += -fno-common -ffunction-sections -fdata-sections
+#CFLAGS += -I $(INCLUDES)
+
+CPPFLAGS += $(DEFS)
+
+# Linker flags
+LDFLAGS += -Wl,--gc-sections
+LDFLAGS += -Wl,--cref
+
+
+!cc = |> ^ CC %f^ $(CC) $(CFLAGS) $(CPPFLAGS) -c %f -o %o |> %B.o
+!LINK = |> ^ LINK %o^ $(LD) $(CFLAGS) $(LDFLAGS) -Wl,-Map=%O.map %f $(LDLIBS) -o %o |> | %O.map
+!OBJCOPY= |> ^ OBJCOPY %o^ $(OBJCOPY) -Oihex %f %o |>
+!OBJDUMP= |> ^ OBJDUMP %o^ $(OBJDUMP) -h -S %f > %o |> %O.lss
+!SIZE = |> ^ SIZE^ $(SIZE) %f |>
+
+: foreach $(SRC) | ../z180/hdrom.h |> !cc |> {objs}
+: $(SRC_Z) |> !cc -D'const=const __flash' |> {objs}
+
+: {objs} |> !LINK |> $(PROG).elf
+: $(PROG).elf |> !OBJCOPY |> %B.hex
+: $(PROG).elf |> !OBJDUMP |> %B.lss
+: $(PROG).elf |> !SIZE |>
+
diff --git a/avr/background.c b/avr/background.c
new file mode 100644
index 0000000..37e4b02
--- /dev/null
+++ b/avr/background.c
@@ -0,0 +1,31 @@
+#include "common.h"
+#include "background.h"
+
+
+#define BG_FUNC_MAX 5
+
+static bg_func func_tab[BG_FUNC_MAX];
+static int_fast8_t fcount;
+
+int bg_register(bg_func f)
+{
+ if (fcount < BG_FUNC_MAX) {
+ func_tab[fcount++] = f;
+ return 1;
+ }
+ return 0;
+}
+
+
+void bg_shed(void)
+{
+ static int_fast8_t current;
+
+ if (func_tab[current]) {
+ func_tab[current](0);
+ }
+ if (++current >= fcount)
+ current = 0;
+}
+
+
diff --git a/avr/background.h b/avr/background.h
new file mode 100644
index 0000000..a624dbb
--- /dev/null
+++ b/avr/background.h
@@ -0,0 +1,10 @@
+#ifndef BACKGROUND_H
+#define BACKGROUND_H
+
+typedef int (*bg_func)(int);
+
+int bg_register(bg_func f);
+void bg_shed(void);
+
+#endif /* BACKGROUND_H */
+
diff --git a/avr/cli.c b/avr/cli.c
new file mode 100644
index 0000000..4b77499
--- /dev/null
+++ b/avr/cli.c
@@ -0,0 +1,356 @@
+#include "common.h"
+
+#include <string.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "config.h"
+#include "command.h"
+#include "xmalloc.h"
+#include "debug.h"
+#include "env.h"
+#include "cli_readline.h"
+#include "con-utils.h"
+#include "cli.h"
+
+#define DEBUG_PARSER 0 /* set to 1 to debug */
+
+#define debug_parser(fmt, args...) \
+ debug_cond(DEBUG_PARSER, fmt, ##args)
+
+static int cli_parse_line(char *line, char *argv[])
+{
+ static const FLASH char delim[] = {" \t"};
+
+ char *ptr;
+ uint_fast8_t nargs = 0;
+
+ debug_parser("parse_line: \"%s\"\n", line);
+
+ ptr = strtok_P(line, delim);
+ while(nargs < CONFIG_SYS_MAXARGS && ptr != NULL) {
+ argv[nargs++] = ptr;
+ ptr = strtok_P(NULL, delim);
+ }
+
+ if (ptr != NULL)
+ printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS);
+
+ argv[nargs] = NULL;
+ debug_parser("parse_line: nargs=%d\n", nargs);
+
+ return nargs;
+}
+
+static void process_macros(const char *input, char *output)
+{
+ char c, prev;
+ const char *varname_start = NULL;
+#if 0 && (CONFIG_SYS_CBSIZE < __UINT_FAST8_MAX__)
+ uint_fast8_t inputcnt = strlen(input);
+ uint_fast8_t outputcnt = CONFIG_SYS_CBSIZE;
+#else
+ int inputcnt = strlen(input);
+ int outputcnt = CONFIG_SYS_CBSIZE;
+#endif
+ uint_fast8_t state = 0; /* 0 = waiting for '$' */
+
+ /* 1 = waiting for '(' or '{' */
+ /* 2 = waiting for ')' or '}' */
+ /* 3 = waiting for ''' */
+ char *output_start = output;
+
+ debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input),
+ input);
+
+ prev = '\0'; /* previous character */
+
+ while (inputcnt && outputcnt) {
+ c = *input++;
+ inputcnt--;
+
+ if (state != 3) {
+ /* remove one level of escape characters */
+ if ((c == '\\') && (prev != '\\')) {
+ if (inputcnt-- == 0)
+ break;
+ prev = c;
+ c = *input++;
+ }
+ }
+
+ switch (state) {
+ case 0: /* Waiting for (unescaped) $ */
+ if ((c == '\'') && (prev != '\\')) {
+ state = 3;
+ break;
+ }
+ if ((c == '$') && (prev != '\\')) {
+ state++;
+ } else {
+ *(output++) = c;
+ outputcnt--;
+ }
+ break;
+ case 1: /* Waiting for ( */
+ if (c == '(' || c == '{') {
+ state++;
+ varname_start = input;
+ } else {
+ state = 0;
+ *(output++) = '$';
+ outputcnt--;
+
+ if (outputcnt) {
+ *(output++) = c;
+ outputcnt--;
+ }
+ }
+ break;
+ case 2: /* Waiting for ) */
+ if (c == ')' || c == '}') {
+ char envname[CONFIG_SYS_ENV_NAMELEN+1], *envval;
+ /* Varname # of chars */
+ uint_fast8_t envcnt = input - varname_start - 1;
+ if (envcnt > CONFIG_SYS_ENV_NAMELEN)
+ envcnt = CONFIG_SYS_ENV_NAMELEN;
+
+ memcpy(envname, varname_start, envcnt);
+ envname[envcnt] = '\0';
+
+ /* Get its value */
+ envval = getenv(envname);
+
+ /* Copy into the line if it exists */
+ if (envval != NULL)
+ while ((*envval) && outputcnt) {
+ *(output++) = *(envval++);
+ outputcnt--;
+ }
+ /* Look for another '$' */
+ state = 0;
+ }
+ break;
+ case 3: /* Waiting for ' */
+ if ((c == '\'') && (prev != '\\')) {
+ state = 0;
+ } else {
+ *(output++) = c;
+ outputcnt--;
+ }
+ break;
+ }
+ prev = c;
+ }
+
+ if (outputcnt)
+ *output = 0;
+ else
+ *(output - 1) = 0;
+
+ debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
+ strlen(output_start), output_start);
+}
+
+ /*
+ * WARNING:
+ *
+ * We must create a temporary copy of the command since the command we get
+ * may be the result from getenv(), which returns a pointer directly to
+ * the environment data, which may change magicly when the command we run
+ * creates or modifies environment variables (like "bootp" does).
+ */
+static int cli_run_command(const char *cmd, int flag)
+{
+ char *cmdbuf; /* working copy of cmd */
+ char *token; /* start of token in cmdbuf */
+ char *sep; /* end of token (separator) in cmdbuf */
+ char *finaltoken;
+ char *str;
+ char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
+ int argc;
+ uint_fast8_t inquotes, repeatable = 1;
+ int rc = 0;
+
+ debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
+ cmd, cmd ? cmd : "NULL");
+
+ clear_ctrlc(); /* forget any previous Control C */
+
+ if (!cmd || !*cmd)
+ return -1; /* empty command */
+
+
+ cmdbuf = xmalloc(strlen(cmd) + 1);
+ finaltoken = xmalloc(CONFIG_SYS_CBSIZE);
+
+ strcpy(cmdbuf, cmd);
+ str = cmdbuf;
+
+ /* Process separators and check for invalid
+ * repeatable commands
+ */
+
+ debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
+ while (*str) {
+ /*
+ * Find separator, or string end
+ * Allow simple escape of ';' by writing "\;"
+ */
+ for (inquotes = 0, sep = str; *sep; sep++) {
+ if ((*sep == '\'') &&
+ (*(sep - 1) != '\\'))
+ inquotes = !inquotes;
+
+ if (!inquotes &&
+ (*sep == ';' || *sep == '\n') && /* separator */
+ (sep != str) && /* past string start */
+ (*(sep - 1) != '\\')) /* and NOT escaped */
+ break;
+ }
+
+ /*
+ * Limit the token to data between separators
+ */
+ token = str;
+ if (*sep) {
+ str = sep + 1; /* start of command for next pass */
+ *sep = '\0';
+ } else {
+ str = sep; /* no more commands for next pass */
+ }
+ debug_parser("token: \"%s\"\n", token);
+
+ /* find macros in this token and replace them */
+ process_macros(token, finaltoken);
+
+ /* Extract arguments */
+ argc = cli_parse_line(finaltoken, argv);
+ if (argc == 0) {
+ rc = -1; /* no command at all */
+ continue;
+ }
+
+ if (cmd_process(flag, argc, argv, &repeatable))
+ rc = -1;
+
+ /* Did the user stop this? */
+ if (had_ctrlc()) {
+ rc = -1; /* if stopped then not repeatable */
+ break;
+ }
+ }
+
+ free(cmdbuf);
+ free(finaltoken);
+
+ return rc ? rc : repeatable;
+}
+
+static int cli_run_command_list(const char *cmd)
+{
+ return (cli_run_command(cmd, 0) < 0);
+}
+
+/******************************************************************************/
+
+
+/*
+ * Run a command using the selected parser.
+ *
+ * @param cmd Command to run
+ * @param flag Execution flags (CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */
+int run_command(const char *cmd, int flag)
+{
+ /*
+ * cli_run_command can return 0 or 1 for success, so clean up
+ * its result.
+ */
+ if (cli_run_command(cmd, flag) == -1)
+ return 1;
+
+ return 0;
+}
+
+/*
+ * Run a command using the selected parser, and check if it is repeatable.
+ *
+ * @param cmd Command to run
+ * @param flag Execution flags (CMD_FLAG_...)
+ * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
+ */
+static int run_command_repeatable(const char *cmd, int flag)
+{
+ return cli_run_command(cmd, flag);
+}
+
+int run_command_list(const char *cmd, int len)
+{
+ (void) len;
+
+ return cli_run_command_list(cmd);
+}
+
+/****************************************************************************/
+
+
+void cli_loop(void)
+{
+ char *lastcommand = NULL;
+ int len;
+ int flag;
+ int rc = 1;
+
+ for (;;) {
+ len = cli_readline(PSTR(CONFIG_SYS_PROMPT));
+
+ flag = 0; /* assume no special flags for now */
+ if (len > 0) {
+ lastcommand = (char *) xrealloc(lastcommand, len+1);
+ if (lastcommand != NULL) {
+ strncpy(lastcommand, console_buffer, len+1);
+ lastcommand[len] = '\0';
+ }
+ } else if (len == 0)
+ flag |= CMD_FLAG_REPEAT;
+
+ if (len == -1)
+ my_puts_P(PSTR("<INTERRUPT>\n"));
+ else
+ rc = run_command_repeatable(lastcommand, flag);
+
+ if (rc <= 0) {
+ /* invalid command or not repeatable, forget it */
+ free(lastcommand);
+ lastcommand = NULL;
+ }
+ }
+}
+
+
+int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ int i;
+ (void) cmdtp;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+ for (i = 1; i < argc; ++i) {
+ char *arg;
+
+ arg = getenv(argv[i]);
+ if (arg == NULL) {
+ printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
+ return 1;
+ }
+
+ if (run_command(arg, flag) != 0)
+ return 1;
+ }
+ return 0;
+}
+
diff --git a/avr/cli.h b/avr/cli.h
new file mode 100644
index 0000000..67ff63b
--- /dev/null
+++ b/avr/cli.h
@@ -0,0 +1,64 @@
+#ifndef CLI_H
+#define CLI_H
+
+/**
+ * Go into the command loop
+ *
+ * This will return if we get a timeout waiting for a command, but only for
+ * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME.
+ */
+void cli_loop(void);
+
+/**
+ * cli_simple_run_command() - Execute a command with the simple CLI
+ *
+ * @cmd: String containing the command to execute
+ * @flag Flag value - see CMD_FLAG_...
+ * @return 1 - command executed, repeatable
+ * 0 - command executed but not repeatable, interrupted commands are
+ * always considered not repeatable
+ * -1 - not executed (unrecognized, bootd recursion or too many args)
+ * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
+ * considered unrecognized)
+ */
+//int cli_simple_run_command(const char *cmd, int flag);
+
+/**
+ * cli_simple_run_command_list() - Execute a list of command
+ *
+ * The commands should be separated by ; or \n and will be executed
+ * by the built-in parser.
+ *
+ * This function cannot take a const char * for the command, since if it
+ * finds newlines in the string, it replaces them with \0.
+ *
+ * @param cmd String containing list of commands
+ * @param flag Execution flags (CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */
+//int cli_simple_run_command_list(char *cmd, int flag);
+
+/**
+ * parse_line() - split a command line down into separate arguments
+ *
+ * The argv[] array is filled with pointers into @line, and each argument
+ * is terminated by \0 (i.e. @line is changed in the process unless there
+ * is only one argument).
+ *
+ * #argv is terminated by a NULL after the last argument pointer.
+ *
+ * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more
+ * than that then an error is printed, and this function returns
+ * CONFIG_SYS_MAXARGS, with argv[] set up to that point.
+ *
+ * @line: Command line to parse
+ * @args: Array to hold arguments
+ * @return number of arguments
+ */
+//int cli_simple_parse_line(char *line, char *argv[]);
+
+int run_command_list(const char *cmd, int len);
+
+
+#endif /* CLI_H */
+
diff --git a/avr/cli_readline.c b/avr/cli_readline.c
new file mode 100644
index 0000000..17d5494
--- /dev/null
+++ b/avr/cli_readline.c
@@ -0,0 +1,595 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * Add to readline cmdline-editing by
+ * (C) Copyright 2005
+ * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "common.h"
+
+#include <avr/pgmspace.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "config.h"
+#include "con-utils.h"
+#include "command.h"
+#include "cli_readline.h"
+
+char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
+
+static const FLASH char erase_seq[] = "\b \b"; /* erase sequence */
+static const FLASH char tab_seq[] = " "; /* used to expand TABs */
+
+#ifndef CONFIG_CMDLINE_EDITING
+static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
+{
+ char *s;
+
+ if (*np == 0)
+ return p;
+
+ if (*(--p) == '\t') { /* will retype the whole line */
+ while (*colp > plen) {
+ my_puts_P(erase_seq);
+ (*colp)--;
+ }
+ for (s = buffer; s < p; ++s) {
+ if (*s == '\t') {
+ my_puts_P(tab_seq + ((*colp) & 07));
+ *colp += 8 - ((*colp) & 07);
+ } else {
+ ++(*colp);
+ putchar(*s);
+ }
+ }
+ } else {
+ my_puts_P(erase_seq);
+ (*colp)--;
+ }
+ (*np)--;
+
+ return p;
+}
+#endif /* CONFIG_CMDLINE_EDITING */
+
+
+#ifdef CONFIG_CMDLINE_EDITING
+
+/*
+ * cmdline-editing related codes from vivi.
+ * Author: Janghoon Lyu <nandy@mizi.com>
+ */
+
+#define putnstr(str, n) printf_P(PSTR("%.*s"), (int)n, str)
+
+#define CTL_CH(c) ((c) - 'a' + 1)
+#define CTL_BACKSPACE ('\b')
+#define DEL ((char)255)
+#define DEL7 ((char)127)
+#define CREAD_HIST_CHAR ('!')
+
+#define getcmd_putch(ch) putchar(ch)
+#define getcmd_getch() my_getchar()
+#define getcmd_cbeep() getcmd_putch('\a')
+
+#define HIST_MAX 5
+#define HIST_SIZE CONFIG_SYS_CBSIZE
+
+static int hist_max;
+static int hist_add_idx;
+static int hist_cur = -1;
+static unsigned hist_num;
+
+static char *hist_list[HIST_MAX];
+static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
+
+#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
+
+static void hist_init(void)
+{
+ int i;
+
+ hist_max = 0;
+ hist_add_idx = 0;
+ hist_cur = -1;
+ hist_num = 0;
+
+ for (i = 0; i < HIST_MAX; i++) {
+ hist_list[i] = hist_lines[i];
+ hist_list[i][0] = '\0';
+ }
+}
+
+static void cread_add_to_hist(char *line)
+{
+ strcpy(hist_list[hist_add_idx], line);
+
+ if (++hist_add_idx >= HIST_MAX)
+ hist_add_idx = 0;
+
+ if (hist_add_idx > hist_max)
+ hist_max = hist_add_idx;
+
+ hist_num++;
+}
+
+static char *hist_prev(void)
+{
+ char *ret;
+ int old_cur;
+
+ if (hist_cur < 0)
+ return NULL;
+
+ old_cur = hist_cur;
+ if (--hist_cur < 0)
+ hist_cur = hist_max;
+
+ if (hist_cur == hist_add_idx) {
+ hist_cur = old_cur;
+ ret = NULL;
+ } else {
+ ret = hist_list[hist_cur];
+ }
+
+ return ret;
+}
+
+static char *hist_next(void)
+{
+ char *ret;
+
+ if (hist_cur < 0)
+ return NULL;
+
+ if (hist_cur == hist_add_idx)
+ return NULL;
+
+ if (++hist_cur > hist_max)
+ hist_cur = 0;
+
+ if (hist_cur == hist_add_idx)
+ ret = "";
+ else
+ ret = hist_list[hist_cur];
+
+ return ret;
+}
+
+#ifndef CONFIG_CMDLINE_EDITING
+static void cread_print_hist_list(void)
+{
+ int i;
+ unsigned n;
+
+ n = hist_num - hist_max;
+
+ i = hist_add_idx + 1;
+ while (1) {
+ if (i > hist_max)
+ i = 0;
+ if (i == hist_add_idx)
+ break;
+ printf_P(PSTR("%s\n"), hist_list[i]);
+ n++;
+ i++;
+ }
+}
+#endif /* CONFIG_CMDLINE_EDITING */
+
+#define BEGINNING_OF_LINE() { \
+ while (num) { \
+ getcmd_putch(CTL_BACKSPACE); \
+ num--; \
+ } \
+}
+
+#define ERASE_TO_EOL() { \
+ if (num < eol_num) { \
+ printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); \
+ do { \
+ getcmd_putch(CTL_BACKSPACE); \
+ } while (--eol_num > num); \
+ } \
+}
+
+#define REFRESH_TO_EOL() { \
+ if (num < eol_num) { \
+ wlen = eol_num - num; \
+ putnstr(buf + num, wlen); \
+ num = eol_num; \
+ } \
+}
+
+static void cread_add_char(char ichar, int insert, unsigned long *num,
+ unsigned long *eol_num, char *buf, unsigned long len)
+{
+ unsigned long wlen;
+
+ /* room ??? */
+ if (insert || *num == *eol_num) {
+ if (*eol_num > len - 1) {
+ getcmd_cbeep();
+ return;
+ }
+ (*eol_num)++;
+ }
+
+ if (insert) {
+ wlen = *eol_num - *num;
+ if (wlen > 1)
+ memmove(&buf[*num+1], &buf[*num], wlen-1);
+
+ buf[*num] = ichar;
+ putnstr(buf + *num, wlen);
+ (*num)++;
+ while (--wlen)
+ getcmd_putch(CTL_BACKSPACE);
+ } else {
+ /* echo the character */
+ wlen = 1;
+ buf[*num] = ichar;
+ putnstr(buf + *num, wlen);
+ (*num)++;
+ }
+}
+
+static void cread_add_str(char *str, int strsize, int insert,
+ unsigned long *num, unsigned long *eol_num,
+ char *buf, unsigned long len)
+{
+ while (strsize--) {
+ cread_add_char(*str, insert, num, eol_num, buf, len);
+ str++;
+ }
+}
+
+static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *len)
+{
+ unsigned long num = 0;
+ unsigned long eol_num = 0;
+ unsigned long wlen;
+ char ichar;
+ int insert = 1;
+ int esc_len = 0;
+ char esc_save[8];
+ int init_len = strlen(buf);
+
+ (void) prompt;
+
+ if (init_len)
+ cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
+
+ while (1) {
+ ichar = getcmd_getch();
+
+ if ((ichar == '\n') || (ichar == '\r')) {
+ putchar('\n');
+ break;
+ }
+
+ /*
+ * handle standard linux xterm esc sequences for arrow key, etc.
+ */
+ if (esc_len != 0) {
+ if (esc_len == 1) {
+ if (ichar == '[') {
+ esc_save[esc_len] = ichar;
+ esc_len = 2;
+ } else {
+ cread_add_str(esc_save, esc_len,
+ insert, &num, &eol_num,
+ buf, *len);
+ esc_len = 0;
+ }
+ continue;
+ }
+
+ switch (ichar) {
+ case 'D': /* <- key */
+ ichar = CTL_CH('b');
+ esc_len = 0;
+ break;
+ case 'C': /* -> key */
+ ichar = CTL_CH('f');
+ esc_len = 0;
+ break; /* pass off to ^F handler */
+ case 'H': /* Home key */
+ ichar = CTL_CH('a');
+ esc_len = 0;
+ break; /* pass off to ^A handler */
+ case 'A': /* up arrow */
+ ichar = CTL_CH('p');
+ esc_len = 0;
+ break; /* pass off to ^P handler */
+ case 'B': /* down arrow */
+ ichar = CTL_CH('n');
+ esc_len = 0;
+ break; /* pass off to ^N handler */
+ default:
+ esc_save[esc_len++] = ichar;
+ cread_add_str(esc_save, esc_len, insert,
+ &num, &eol_num, buf, *len);
+ esc_len = 0;
+ continue;
+ }
+ }
+
+ switch (ichar) {
+ case 0x1b:
+ if (esc_len == 0) {
+ esc_save[esc_len] = ichar;
+ esc_len = 1;
+ } else {
+ my_puts_P(PSTR("impossible condition #876\n"));
+ esc_len = 0;
+ }
+ break;
+
+ case CTL_CH('a'):
+ BEGINNING_OF_LINE();
+ break;
+ case CTL_CH('c'): /* ^C - break */
+ *buf = '\0'; /* discard input */
+ return -1;
+ case CTL_CH('f'):
+ if (num < eol_num) {
+ getcmd_putch(buf[num]);
+ num++;
+ }
+ break;
+ case CTL_CH('b'):
+ if (num) {
+ getcmd_putch(CTL_BACKSPACE);
+ num--;
+ }
+ break;
+ case CTL_CH('d'):
+ if (num < eol_num) {
+ wlen = eol_num - num - 1;
+ if (wlen) {
+ memmove(&buf[num], &buf[num+1], wlen);
+ putnstr(buf + num, wlen);
+ }
+
+ getcmd_putch(' ');
+ do {
+ getcmd_putch(CTL_BACKSPACE);
+ } while (wlen--);
+ eol_num--;
+ }
+ break;
+ case CTL_CH('k'):
+ ERASE_TO_EOL();
+ break;
+ case CTL_CH('e'):
+ REFRESH_TO_EOL();
+ break;
+ case CTL_CH('o'):
+ insert = !insert;
+ break;
+ case CTL_CH('x'):
+ case CTL_CH('u'):
+ BEGINNING_OF_LINE();
+ ERASE_TO_EOL();
+ break;
+ case DEL:
+ case DEL7:
+ case 8:
+ if (num) {
+ wlen = eol_num - num;
+ num--;
+ memmove(&buf[num], &buf[num+1], wlen);
+ getcmd_putch(CTL_BACKSPACE);
+ putnstr(buf + num, wlen);
+ getcmd_putch(' ');
+ do {
+ getcmd_putch(CTL_BACKSPACE);
+ } while (wlen--);
+ eol_num--;
+ }
+ break;
+ case CTL_CH('p'):
+ case CTL_CH('n'):
+ {
+ char *hline;
+
+ esc_len = 0;
+
+ if (ichar == CTL_CH('p'))
+ hline = hist_prev();
+ else
+ hline = hist_next();
+
+ if (!hline) {
+ getcmd_cbeep();
+ continue;
+ }
+
+ /* nuke the current line */
+ /* first, go home */
+ BEGINNING_OF_LINE();
+
+ /* erase to end of line */
+ ERASE_TO_EOL();
+
+ /* copy new line into place and display */
+ strcpy(buf, hline);
+ eol_num = strlen(buf);
+ REFRESH_TO_EOL();
+ continue;
+ }
+#ifdef CONFIG_AUTO_COMPLETE
+ case '\t': {
+ int num2, col;
+
+ /* do not autocomplete when in the middle */
+ if (num < eol_num) {
+ getcmd_cbeep();
+ break;
+ }
+
+ buf[num] = '\0';
+ col = strlen_P(prompt) + eol_num;
+ num2 = num;
+ if (cmd_auto_complete(prompt, buf, &num2, &col)) {
+ col = num2 - num;
+ num += col;
+ eol_num += col;
+ }
+ break;
+ }
+#endif
+ default:
+ cread_add_char(ichar, insert, &num, &eol_num, buf,
+ *len);
+ break;
+ }
+ }
+ *len = eol_num;
+ buf[eol_num] = '\0'; /* lose the newline */
+
+ if (buf[0] && buf[0] != CREAD_HIST_CHAR)
+ cread_add_to_hist(buf);
+ hist_cur = hist_add_idx;
+
+ return 0;
+}
+
+#endif /* CONFIG_CMDLINE_EDITING */
+
+/****************************************************************************/
+
+static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer)
+{
+ char *p = buffer;
+#ifdef CONFIG_CMDLINE_EDITING
+ unsigned int len = CONFIG_SYS_CBSIZE;
+ int rc;
+ static int initted;
+
+ if (!initted) {
+ hist_init();
+ initted = 1;
+ }
+
+ if (prompt)
+ my_puts_P(prompt);
+
+ rc = cread_line(prompt, p, &len);
+ return rc < 0 ? rc : (int) len;
+
+#else /* CONFIG_CMDLINE_EDITING */
+ char *p_buf = p;
+ int n = 0; /* buffer index */
+ int plen = 0; /* prompt length */
+ int col; /* output column cnt */
+ char c;
+
+ /* print prompt */
+ if (prompt) {
+ plen = strlen_P(prompt);
+ my_puts_P(prompt);
+ }
+ col = plen;
+
+ for (;;) {
+
+ c = my_getchar();
+
+ /*
+ * Special character handling
+ */
+ switch (c) {
+ case '\r': /* Enter */
+ case '\n':
+ *p = '\0';
+ my_puts_P(PSTR("\r\n"));
+ return p - p_buf;
+
+ case '\0': /* nul */
+ continue;
+
+ case 0x03: /* ^C - break */
+ p_buf[0] = '\0'; /* discard input */
+ return -1;
+
+ case 0x15: /* ^U - erase line */
+ while (col > plen) {
+ my_puts_P(erase_seq);
+ --col;
+ }
+ p = p_buf;
+ n = 0;
+ continue;
+
+ case 0x17: /* ^W - erase word */
+ p = delete_char(p_buf, p, &col, &n, plen);
+ while ((n > 0) && (*p != ' '))
+ p = delete_char(p_buf, p, &col, &n, plen);
+ continue;
+
+ case 0x08: /* ^H - backspace */
+ case 0x7F: /* DEL - backspace */
+ p = delete_char(p_buf, p, &col, &n, plen);
+ continue;
+
+ default:
+ /*
+ * Must be a normal character then
+ */
+ if (n < CONFIG_SYS_CBSIZE-2) {
+ if (c == '\t') { /* expand TABs */
+#ifdef CONFIG_AUTO_COMPLETE
+ /*
+ * if auto completion triggered just
+ * continue
+ */
+ *p = '\0';
+ if (cmd_auto_complete(prompt,
+ console_buffer,
+ &n, &col)) {
+ p = p_buf + n; /* reset */
+ continue;
+ }
+#endif
+ my_puts_P(tab_seq + (col & 07));
+ col += 8 - (col & 07);
+ } else {
+ char buf[2];
+
+ /*
+ * Echo input using puts() to force an
+ * LCD flush if we are using an LCD
+ */
+ ++col;
+ buf[0] = c;
+ buf[1] = '\0';
+ my_puts(buf);
+ }
+ *p++ = c;
+ ++n;
+ } else { /* Buffer full */
+ putchar('\a');
+ }
+ }
+ }
+#endif /* CONFIG_CMDLINE_EDITING */
+}
+
+int cli_readline(const FLASH char *const prompt)
+{
+ /*
+ * If console_buffer isn't 0-length the user will be prompted to modify
+ * it instead of entering it from scratch as desired.
+ */
+ console_buffer[0] = '\0';
+
+ return cli_readline_into_buffer(prompt, console_buffer);
+}
+
diff --git a/avr/cli_readline.h b/avr/cli_readline.h
new file mode 100644
index 0000000..5b25762
--- /dev/null
+++ b/avr/cli_readline.h
@@ -0,0 +1,50 @@
+/*
+ * (C) Copyright 2014 Google, Inc
+ * Simon Glass <sjg@chromium.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#ifndef CLI_READLINE_H
+#define CLI_READLINE_H
+
+extern char console_buffer[]; /* console I/O buffer */
+
+/**
+ * cli_readline() - read a line into the console_buffer
+ *
+ * This is a convenience function which calls cli_readline_into_buffer().
+ *
+ * @prompt: Prompt to display
+ * @return command line length excluding terminator, or -ve on error
+ */
+int cli_readline(const FLASH char *const prompt);
+
+/**
+ * readline_into_buffer() - read a line into a buffer
+ *
+ * Display the prompt, then read a command line into @buffer. The
+ * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which
+ * will always be added.
+ *
+ * The command is echoed as it is typed. Command editing is supported if
+ * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if
+ * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined,
+ * then a timeout will be applied.
+ *
+ * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
+ * time out when time goes past endtime (timebase time in ticks).
+ *
+ * @prompt: Prompt to display
+ * @buffer: Place to put the line that is entered
+ * @timeout: Timeout in milliseconds, 0 if none
+ * @return command line length excluding terminator, or -ve on error: of the
+ * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
+ * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
+ * -1 is returned.
+ */
+//int cli_readline_into_buffer(const char *const prompt, char *buffer, int timeout);
+
+
+#endif /* CLI_READLINE_H */
+
diff --git a/avr/cmd_boot.c b/avr/cmd_boot.c
new file mode 100644
index 0000000..1699a5c
--- /dev/null
+++ b/avr/cmd_boot.c
@@ -0,0 +1,145 @@
+
+/*
+ * Misc boot support
+ */
+#include "common.h"
+#include <stdlib.h>
+#include <util/delay.h>
+#include <avr/pgmspace.h>
+
+#include "command.h"
+#include "z80-if.h"
+
+/* ugly hack to get Z180 loadfile into flash memory */
+#define const const FLASH
+#include "../z180/hdrom.h"
+#undef const
+
+
+
+static void z80_load_mem(void)
+{
+ unsigned sec = 0;
+ uint32_t sec_base = hdrom_start;
+
+ printf_P(PSTR("Loading Z180 memory... \n"));
+
+ while (sec < hdrom_sections) {
+ printf_P(PSTR(" From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n"),
+ hdrom_address[sec],
+ hdrom_address[sec]+hdrom_length_of_sections[sec] - 1,
+ hdrom_length_of_sections[sec]);
+
+ z80_bus_cmd(Request);
+ z80_write_block((const FLASH unsigned char *) &hdrom[sec_base], /* src */
+ hdrom_address[sec], /* dest */
+ hdrom_length_of_sections[sec]); /* len */
+ z80_bus_cmd(Release);
+ sec_base+=hdrom_length_of_sections[sec];
+ sec++;
+ }
+}
+
+int do_loadf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ (void) cmdtp; (void) flag; (void) argc; (void) argv;
+
+ if (z80_bus_state() & ZST_RUNNING) {
+ printf_P(PSTR("## Can't load while CPU is running!\n"));
+ return 1;
+ }
+
+ z80_load_mem();
+
+ return 0;
+}
+
+
+int do_busreq_pulse(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint16_t count=1;
+
+ (void) cmdtp; (void) flag;
+
+ if (!(z80_bus_state() & ZST_RUNNING)) {
+ printf_P(PSTR("## CPU is not running!\n"));
+ return 1;
+ }
+
+ if (argc > 1)
+ count = (uint16_t) strtoul(argv[2], NULL, 16);
+
+ z80_bus_cmd(Request);
+ while (count--)
+ z80_bus_cmd(M_Cycle);
+
+ return 0;
+}
+
+
+int do_go(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint32_t addr;
+
+ (void) cmdtp; (void) flag;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+ addr = strtoul(argv[1], NULL, 16);
+ if (addr >= (1UL<<16)) {
+ printf_P(PSTR("## Startaddress 0x%05lx too high.\n"
+ " (Out of logical address space (0x00000-0x0ffff))\n"),
+ addr);
+ return 1;
+ }
+
+ if (z80_bus_state() & ZST_RUNNING) {
+ printf_P(PSTR("## CPU allready running!\n"));
+ return 1;
+ }
+
+ printf_P(PSTR("## Starting application at 0x%04lx ...\n"), addr);
+
+ if (addr != 0) {
+ uint8_t tmp[3];
+ uint_fast8_t i;
+
+ z80_bus_cmd(Request);
+ for (i = 0; i < 3; i++)
+ tmp[i] = z80_read(i);
+ z80_write(0, 0xc3);
+ z80_write(1, addr);
+ z80_write(2, (addr >> 8));
+
+ z80_bus_cmd(Run);
+ z80_bus_cmd(M_Cycle);
+ z80_bus_cmd(M_Cycle);
+ for (i = 0; i < 3; i++)
+ z80_write(i, tmp[i]);
+ } else
+ z80_bus_cmd(Run);
+
+ z80_bus_cmd(Release);
+
+ return 0;
+}
+
+int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ (void) cmdtp; (void) flag; (void) argc; (void) argv;
+
+ printf_P(PSTR("## CPU now in reset state.\n"));
+
+ z80_bus_cmd(Reset);
+ return 0;
+}
+
+int do_restart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ (void) cmdtp; (void) flag; (void) argc; (void) argv;
+
+ z80_bus_cmd(Restart);
+
+ return 0;
+}
+
diff --git a/avr/cmd_echo.c b/avr/cmd_echo.c
new file mode 100644
index 0000000..d142ab6
--- /dev/null
+++ b/avr/cmd_echo.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2000-2009
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "common.h"
+#include "command.h"
+
+
+int do_echo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint_fast8_t putnl = 1;
+
+ (void) cmdtp; (void) flag;
+
+ for (uint_fast8_t i = 1; i < argc; i++) {
+
+ uint_fast8_t backslash = 0;
+ char *p = argv[i];
+ char c;
+
+ if (i != 1)
+ putchar(' ');
+ while ((c = *p++) != '\0') {
+
+ if(backslash) {
+ backslash = 0;
+ if (c == 'c') {
+ putnl = 0;
+ continue;
+ } else
+ putchar('\\');
+ } else {
+ if (c == '\\') {
+ backslash = 1;
+ continue;
+ }
+ }
+ putchar(c);
+ }
+ }
+
+ if (putnl)
+ putchar('\n');
+
+ return 0;
+}
+
+#if 0
+U_BOOT_CMD(
+ echo, CONFIG_SYS_MAXARGS, 1, do_echo,
+ "echo args to console",
+ "[args..]\n"
+ " - echo args to console; \\c suppresses newline"
+);
+#endif
diff --git a/avr/cmd_help.c b/avr/cmd_help.c
new file mode 100644
index 0000000..317eb5d
--- /dev/null
+++ b/avr/cmd_help.c
@@ -0,0 +1,32 @@
+
+#include "common.h"
+#include "command.h"
+
+int do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ const int len = cmd_tbl_item_count();
+ return _do_help(cmd_tbl, len, cmdtp, flag, argc, argv);
+}
+
+
+#if 0
+
+U_BOOT_CMD(
+ help, CONFIG_SYS_MAXARGS, 1, do_help,
+ "print command description/usage",
+ "\n"
+ " - print brief description of all commands\n"
+ "help command ...\n"
+ " - print detailed usage of 'command'"
+);
+
+/* This does not use the U_BOOT_CMD macro as ? can't be used in symbol names */
+ll_entry_declare(cmd_tbl_t, question_mark, cmd) = {
+ "?", CONFIG_SYS_MAXARGS, 1, do_help,
+ "alias for 'help'",
+#ifdef CONFIG_SYS_LONGHELP
+ ""
+#endif /* CONFIG_SYS_LONGHELP */
+};
+
+#endif
diff --git a/avr/cmd_mem.c b/avr/cmd_mem.c
new file mode 100644
index 0000000..253f02f
--- /dev/null
+++ b/avr/cmd_mem.c
@@ -0,0 +1,878 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+/*
+ * Memory Functions
+ *
+ * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
+ */
+
+#include "common.h"
+#include <stdlib.h>
+#include <ctype.h>
+
+#include "config.h"
+#include "debug.h"
+#include "command.h"
+#include "cli_readline.h"
+#include "con-utils.h"
+#include "z80-if.h"
+
+/*
+ * TODO: printf() --> printf_P()
+ */
+
+#ifndef CONFIG_SYS_MEMTEST_SCRATCH
+#define CONFIG_SYS_MEMTEST_SCRATCH 0
+#endif
+
+static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
+
+/* Display values from last command.
+ * Memory modify remembered values are different from display memory.
+ */
+static uint32_t dp_last_addr;
+static uint32_t dp_last_length = 0x100;
+static uint32_t mm_last_addr;
+
+static uint32_t base_address = 0;
+
+/*--------------------------------------------------------------------------*/
+
+static void print_blanks(uint_fast8_t count)
+{
+ while(count--)
+ putchar(' ');
+}
+
+int z180_dump_mem(uint32_t startaddr, uint32_t len, const char *title)
+{
+ uint8_t buf[16];
+ uint8_t llen = 16;
+ uint8_t pre = startaddr % 16;
+ uint32_t addr = startaddr & ~0x0f;
+ len += pre;
+ uint8_t i;
+
+ if (title && *title)
+ printf_P(PSTR("%s\n"),title);
+
+ while (len) {
+ if (len < 16)
+ llen = len;
+
+ z80_bus_cmd(Request);
+ for (i = pre; i < llen; i++)
+ buf[i] = z80_read(addr + i);
+ z80_bus_cmd(Release);
+
+ printf_P(PSTR("%.5lx:"), addr);
+#if 0
+ print_blanks(3 * pre);
+
+ /* Print hex values */
+ for (i = pre; i < llen; i++)
+ printf_P(PSTR(" %.2x"), buf[i]);
+#else
+ for (i = 0; i < llen; i++) {
+ if ((i % 8) == 0)
+ putchar(' ');
+ if (i < pre)
+ printf_P(PSTR(".. "));
+ else
+ printf_P(PSTR("%.2x "), buf[i]);
+ }
+#endif
+ /* fill line with whitespace for nice ASCII print */
+#if 1
+ print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre);
+#else
+
+#endif
+ /* Print data in ASCII characters */
+ for (i = pre; i < llen; i++)
+ printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
+ putchar('\n');
+
+ pre = 0;
+ addr += 16;
+ len -= llen;
+
+ if (ctrlc())
+ return -1;
+ }
+ return 0;
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+/* Memory Display
+ *
+ * Syntax:
+ * md {addr} {len}
+ */
+int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint32_t addr, length;
+
+ (void) cmdtp;
+
+#if 0
+ printf_P(PSTR("flag: %d, argc: %d"), flag, argc);
+ for (int i = 0; i < argc; i++) {
+ printf_P(PSTR(", argv[%d]: %s"), i, argv[i] ? argv[i] : "<NULL>");
+ }
+ putchar('\n');
+#endif
+
+ /* We use the last specified parameters, unless new ones are
+ * entered.
+ */
+ addr = dp_last_addr;
+ length = dp_last_length;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+ if ((flag & CMD_FLAG_REPEAT) == 0) {
+ /* Address is specified since argc > 1 */
+ addr = strtoul(argv[1], NULL, 16);
+ addr += base_address;
+
+ /* If another parameter, it is the length to display. */
+ if (argc > 2)
+ length = strtoul(argv[2], NULL, 16);
+ }
+
+ /* Print the lines. */
+ z180_dump_mem(addr, length, NULL);
+
+ dp_last_addr = addr + length;
+ dp_last_length = length;
+ return 0;
+}
+
+int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ return mod_mem (cmdtp, 1, flag, argc, argv);
+}
+int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ return mod_mem (cmdtp, 0, flag, argc, argv);
+}
+
+int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint8_t writeval;
+ uint32_t addr, count;
+
+ (void) cmdtp;
+ (void) flag;
+
+ if ((argc < 3) || (argc > 4))
+ return CMD_RET_USAGE;
+
+ /* Address is specified since argc > 1
+ */
+ addr = strtoul(argv[1], NULL, 16);
+ addr += base_address;
+
+ /* Get the value to write.
+ */
+ writeval = (uint8_t) strtoul(argv[2], NULL, 16);
+
+ /* Count ? */
+ if (argc == 4) {
+ count = strtoul(argv[3], NULL, 16);
+ } else {
+ count = 1;
+ }
+
+ z80_bus_cmd(Request);
+ while (count-- > 0) {
+ z80_write(addr, writeval);
+ ++addr;
+ }
+ z80_bus_cmd(Release);
+
+ return 0;
+}
+
+#ifdef CONFIG_MX_CYCLIC
+int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ int i;
+ uint32_t count;
+
+ if (argc < 4)
+ return CMD_RET_USAGE;
+
+ count = strtoul(argv[3], NULL, 10);
+
+ for (;;) {
+ do_mem_md (NULL, 0, 3, argv);
+
+ /* delay for <count> ms... */
+/* TODO: use timer */
+ for (i=0; i<count; i++)
+ udelay (1000);
+
+ /* check for ctrl-c to abort... */
+ if (ctrlc()) {
+ my_puts("Abort\n");
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
+int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ int i;
+ uint32_t count;
+
+ if (argc < 4)
+ return CMD_RET_USAGE;
+
+ count = strtoul(argv[3], NULL, 10);
+
+ for (;;) {
+ do_mem_mw (NULL, 0, 3, argv);
+
+ /* delay for <count> ms... */
+/* TODO: use timer */
+ for (i=0; i<count; i++)
+ udelay (1000);
+
+ /* check for ctrl-c to abort... */
+ if (ctrlc()) {
+ my_puts("Abort\n");
+ return 0;
+ }
+ }
+
+ return 0;
+}
+#endif /* CONFIG_MX_CYCLIC */
+
+int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint32_t addr1, addr2, count, ngood;
+ int rcode = 0;
+ uint8_t byte1, byte2;
+
+ (void) cmdtp;
+ (void) flag;
+
+ if (argc != 4)
+ return CMD_RET_USAGE;
+
+
+ addr1 = strtoul(argv[1], NULL, 16);
+ addr1 += base_address;
+ addr2 = strtoul(argv[2], NULL, 16);
+ addr2 += base_address;
+ count = strtoul(argv[3], NULL, 16);
+
+ for (ngood = 0; ngood < count; ++ngood) {
+ z80_bus_cmd(Request);
+ byte1 = z80_read(addr1);
+ byte2 = z80_read(addr2);
+ z80_bus_cmd(Release);
+ if (byte1 != byte2) {
+ printf( "byte at 0x%05lx (%#02x) != "
+ "byte at 0x%05lx (%#02x)\n",
+ addr1, byte1, addr2, byte2);
+ rcode = 1;
+ break;
+ }
+ addr1++;
+ addr2++;
+
+ /* check for ctrl-c to abort... */
+ if (ctrlc()) {
+ my_puts("Abort\n");
+ return 0;
+ }
+ }
+
+ printf("Total of %ld byte(s) (0x%lx) were the same\n", ngood, ngood);
+ return rcode;
+}
+
+int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint32_t src, dest, count;
+ int_fast8_t step;
+
+ (void) cmdtp;
+ (void) flag;
+
+ if (argc != 4)
+ return CMD_RET_USAGE;
+
+ src = strtoul(argv[1], NULL, 16);
+ src += base_address;
+ dest = strtoul(argv[2], NULL, 16);
+ dest += base_address;
+ count = strtoul(argv[3], NULL, 16);
+
+ if (count == 0) {
+ my_puts ("Zero length ???\n");
+ return 1;
+ }
+
+ if (dest > src) {
+ src += count - 1;
+ dest += count - 1;
+ step = -1;
+ } else
+ step = 1;
+
+ while (count-- > 0) {
+ uint8_t data;
+ z80_bus_cmd(Request);
+ data = z80_read(src);
+ z80_write(dest, data);
+ z80_bus_cmd(Release);
+ src += step;
+ dest += step;
+
+ /* check for ctrl-c to abort... */
+ if (ctrlc()) {
+ my_puts("Abort\n");
+ return 0;
+ }
+ }
+ return 0;
+}
+
+int do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
+{
+ (void) cmdtp;
+ (void) flag;
+
+ if (argc > 1) {
+ /* Set new base address. */
+ base_address = strtoul(argv[1], NULL, 16);
+ }
+ /* Print the current base address. */
+ printf("Base Address: 0x%05lx\n", base_address);
+ return 0;
+}
+
+int do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
+{
+ uint32_t addr, length;
+
+ (void) cmdtp;
+ (void) flag;
+
+ if (argc < 3)
+ return CMD_RET_USAGE;
+
+ /* Address is always specified. */
+ addr = strtoul(argv[1], NULL, 16);
+
+ /* Length is the number of bytes. */
+ length = strtoul(argv[2], NULL, 16);
+
+
+ /* We want to optimize the loops to run as fast as possible.
+ * If we have only one object, just run infinite loops.
+ */
+ if (length == 1) {
+ z80_bus_cmd(Request);
+ for (;;)
+ z80_read(addr);
+ z80_bus_cmd(Release);
+ }
+
+ z80_bus_cmd(Request);
+ for (;;) {
+ uint32_t i = length;
+ uint32_t p = addr;
+ while (i-- > 0)
+ z80_read(p++);
+ }
+ z80_bus_cmd(Release);
+
+ return 0;
+}
+
+#ifdef CONFIG_LOOPW
+int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint32_t addr, length;
+ uint8_t data;
+
+ (void) cmdtp;
+ (void) flag;
+
+ if (argc < 4)
+ return CMD_RET_USAGE;
+
+ /* Address is always specified. */
+ addr = strtoul(argv[1], NULL, 16);
+
+ /* Length is the number of bytes. */
+ length = strtoul(argv[2], NULL, 16);
+
+ data = strtoul(argv[3], NULL, 16);
+
+ /* We want to optimize the loops to run as fast as possible.
+ * If we have only one object, just run infinite loops.
+ */
+ if (length == 1) {
+ z80_bus_cmd(Request);
+ for (;;)
+ z80_write(addr, data);
+ }
+
+ for (;;) {
+ uint32_t i = length;
+ uint32_t p = addr;
+ while (i-- > 0)
+ z80_write(p++, data);
+ }
+}
+#endif /* CONFIG_LOOPW */
+
+#ifdef CONFIG_CMD_MEMTEST
+static uint32_t mem_test_alt(vu_long *buf, uint32_t start_addr, uint32_t end_addr,
+ vu_long *dummy)
+{
+ vu_long *addr;
+ uint32_t errs = 0;
+ uint32_t val, readback;
+ int j;
+ vu_long offset;
+ vu_long test_offset;
+ vu_long pattern;
+ vu_long temp;
+ vu_long anti_pattern;
+ vu_long num_words;
+ static const FLASH uint32_t bitpattern[] = {
+ 0x00000001, /* single bit */
+ 0x00000003, /* two adjacent bits */
+ 0x00000007, /* three adjacent bits */
+ 0x0000000F, /* four adjacent bits */
+ 0x00000005, /* two non-adjacent bits */
+ 0x00000015, /* three non-adjacent bits */
+ 0x00000055, /* four non-adjacent bits */
+ 0xaaaaaaaa, /* alternating 1/0 */
+ };
+
+ num_words = (end_addr - start_addr) / sizeof(vu_long);
+
+ /*
+ * Data line test: write a pattern to the first
+ * location, write the 1's complement to a 'parking'
+ * address (changes the state of the data bus so a
+ * floating bus doesn't give a false OK), and then
+ * read the value back. Note that we read it back
+ * into a variable because the next time we read it,
+ * it might be right (been there, tough to explain to
+ * the quality guys why it prints a failure when the
+ * "is" and "should be" are obviously the same in the
+ * error message).
+ *
+ * Rather than exhaustively testing, we test some
+ * patterns by shifting '1' bits through a field of
+ * '0's and '0' bits through a field of '1's (i.e.
+ * pattern and ~pattern).
+ */
+ addr = buf;
+ for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
+ val = bitpattern[j];
+ for (; val != 0; val <<= 1) {
+ *addr = val;
+ *dummy = ~val; /* clear the test data off the bus */
+ readback = *addr;
+ if (readback != val) {
+ printf("FAILURE (data line): "
+ "expected %05lx, actual %05lx\n",
+ val, readback);
+ errs++;
+ if (ctrlc())
+ return -1;
+ }
+ *addr = ~val;
+ *dummy = val;
+ readback = *addr;
+ if (readback != ~val) {
+ printf("FAILURE (data line): "
+ "Is %05lx, should be %05lx\n",
+ readback, ~val);
+ errs++;
+ if (ctrlc())
+ return -1;
+ }
+ }
+ }
+
+ /*
+ * Based on code whose Original Author and Copyright
+ * information follows: Copyright (c) 1998 by Michael
+ * Barr. This software is placed into the public
+ * domain and may be used for any purpose. However,
+ * this notice must not be changed or removed and no
+ * warranty is either expressed or implied by its
+ * publication or distribution.
+ */
+
+ /*
+ * Address line test
+
+ * Description: Test the address bus wiring in a
+ * memory region by performing a walking
+ * 1's test on the relevant bits of the
+ * address and checking for aliasing.
+ * This test will find single-bit
+ * address failures such as stuck-high,
+ * stuck-low, and shorted pins. The base
+ * address and size of the region are
+ * selected by the caller.
+
+ * Notes: For best results, the selected base
+ * address should have enough LSB 0's to
+ * guarantee single address bit changes.
+ * For example, to test a 64-Kbyte
+ * region, select a base address on a
+ * 64-Kbyte boundary. Also, select the
+ * region size as a power-of-two if at
+ * all possible.
+ *
+ * Returns: 0 if the test succeeds, 1 if the test fails.
+ */
+ pattern = (vu_long) 0xaaaaaaaa;
+ anti_pattern = (vu_long) 0x55555555;
+
+ debug("%s:%d: length = 0x%.5lx\n", __func__, __LINE__, num_words);
+ /*
+ * Write the default pattern at each of the
+ * power-of-two offsets.
+ */
+ for (offset = 1; offset < num_words; offset <<= 1)
+ addr[offset] = pattern;
+
+ /*
+ * Check for address bits stuck high.
+ */
+ test_offset = 0;
+ addr[test_offset] = anti_pattern;
+
+ for (offset = 1; offset < num_words; offset <<= 1) {
+ temp = addr[offset];
+ if (temp != pattern) {
+ printf("\nFAILURE: Address bit stuck high @ 0x%.5lx:"
+ " expected 0x%.5lx, actual 0x%.5lx\n",
+ start_addr + offset*sizeof(vu_long),
+ pattern, temp);
+ errs++;
+ if (ctrlc())
+ return -1;
+ }
+ }
+ addr[test_offset] = pattern;
+
+ /*
+ * Check for addr bits stuck low or shorted.
+ */
+ for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
+ addr[test_offset] = anti_pattern;
+
+ for (offset = 1; offset < num_words; offset <<= 1) {
+ temp = addr[offset];
+ if ((temp != pattern) && (offset != test_offset)) {
+ printf("\nFAILURE: Address bit stuck low or"
+ " shorted @ 0x%.5lx: expected 0x%.5lx,"
+ " actual 0x%.5lx\n",
+ start_addr + offset*sizeof(vu_long),
+ pattern, temp);
+ errs++;
+ if (ctrlc())
+ return -1;
+ }
+ }
+ addr[test_offset] = pattern;
+ }
+
+ /*
+ * Description: Test the integrity of a physical
+ * memory device by performing an
+ * increment/decrement test over the
+ * entire region. In the process every
+ * storage bit in the device is tested
+ * as a zero and a one. The base address
+ * and the size of the region are
+ * selected by the caller.
+ *
+ * Returns: 0 if the test succeeds, 1 if the test fails.
+ */
+ num_words++;
+
+ /*
+ * Fill memory with a known pattern.
+ */
+ for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
+ addr[offset] = pattern;
+ }
+
+ /*
+ * Check each location and invert it for the second pass.
+ */
+ for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
+ temp = addr[offset];
+ if (temp != pattern) {
+ printf("\nFAILURE (read/write) @ 0x%.5lx:"
+ " expected 0x%.5lx, actual 0x%.5lx)\n",
+ start_addr + offset*sizeof(vu_long),
+ pattern, temp);
+ errs++;
+ if (ctrlc())
+ return -1;
+ }
+
+ anti_pattern = ~pattern;
+ addr[offset] = anti_pattern;
+ }
+
+ /*
+ * Check each location for the inverted pattern and zero it.
+ */
+ for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
+ WATCHDOG_RESET();
+ anti_pattern = ~pattern;
+ temp = addr[offset];
+ if (temp != anti_pattern) {
+ printf("\nFAILURE (read/write): @ 0x%.5lx:"
+ " expected 0x%.5lx, actual 0x%.5lx)\n",
+ start_addr + offset*sizeof(vu_long),
+ anti_pattern, temp);
+ errs++;
+ if (ctrlc())
+ return -1;
+ }
+ addr[offset] = 0;
+ }
+
+ return 0;
+}
+
+static uint32_t mem_test_quick(vu_long *buf, uint32_t start_addr, uint32_t end_addr,
+ vu_long pattern, int iteration)
+{
+ vu_long *end;
+ vu_long *addr;
+ uint32_t errs = 0;
+ uint32_t incr, length;
+ uint32_t val, readback;
+
+ /* Alternate the pattern */
+ incr = 1;
+ if (iteration & 1) {
+ incr = -incr;
+ /*
+ * Flip the pattern each time to make lots of zeros and
+ * then, the next time, lots of ones. We decrement
+ * the "negative" patterns and increment the "positive"
+ * patterns to preserve this feature.
+ */
+ if (pattern & 0x80000000)
+ pattern = -pattern; /* complement & increment */
+ else
+ pattern = ~pattern;
+ }
+ length = (end_addr - start_addr) / sizeof(uint32_t);
+ end = buf + length;
+ printf("\rPattern %08lX Writing..."
+ "%12s"
+ "\b\b\b\b\b\b\b\b\b\b",
+ pattern, "");
+
+ for (addr = buf, val = pattern; addr < end; addr++) {
+ *addr = val;
+ val += incr;
+ }
+
+ my_puts("Reading...");
+
+ for (addr = buf, val = pattern; addr < end; addr++) {
+ readback = *addr;
+ if (readback != val) {
+ uint32_t offset = addr - buf;
+
+ printf("\nMem error @ 0x%08X: "
+ "found %08lX, expected %08lX\n",
+ (unsigned int)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
+ readback, val);
+ errs++;
+ if (ctrlc())
+ return -1;
+ }
+ val += incr;
+ }
+
+ return 0;
+}
+
+/*
+ * Perform a memory test. A more complete alternative test can be
+ * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
+ * interrupted by ctrl-c or by a failure of one of the sub-tests.
+ */
+int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
+{
+ uint32_t start, end;
+ vu_long *buf, *dummy;
+ int iteration_limit;
+ int ret;
+ uint32_t errs = 0; /* number of errors, or -1 if interrupted */
+ uint32_t pattern;
+ int iteration;
+#if defined(CONFIG_SYS_ALT_MEMTEST)
+ const int alt_test = 1;
+#else
+ const int alt_test = 0;
+#endif
+
+ if (argc > 1)
+ start = strtoul(argv[1], NULL, 16);
+ else
+ start = CONFIG_SYS_MEMTEST_START;
+
+ if (argc > 2)
+ end = strtoul(argv[2], NULL, 16);
+ else
+ end = CONFIG_SYS_MEMTEST_END;
+
+ if (argc > 3)
+ pattern = (uint32_t)strtoul(argv[3], NULL, 16);
+ else
+ pattern = 0;
+
+ if (argc > 4)
+ iteration_limit = (uint32_t)strtoul(argv[4], NULL, 16);
+ else
+ iteration_limit = 0;
+
+ printf("Testing %08x ... %08x:\n", (unsigned int)start, (unsigned int)end);
+ debug("%s:%d: start %#05lx end %#05lx\n", __func__, __LINE__,
+ start, end);
+
+/* TODO: */
+// buf = map_sysmem(start, end - start);
+// dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
+ for (iteration = 0;
+ !iteration_limit || iteration < iteration_limit;
+ iteration++) {
+ if (ctrlc()) {
+ errs = -1UL;
+ break;
+ }
+
+ printf("Iteration: %6d\r", iteration + 1);
+ debug("\n");
+ if (alt_test) {
+ errs = mem_test_alt(buf, start, end, dummy);
+ } else {
+ errs = mem_test_quick(buf, start, end, pattern,
+ iteration);
+ }
+ if (errs == -1UL)
+ break;
+ }
+
+ if (errs == -1UL) {
+ /* Memory test was aborted - write a newline to finish off */
+ putc('\n');
+ ret = 1;
+ } else {
+ printf("Tested %d iteration(s) with %lu errors.\n",
+ iteration, errs);
+ ret = errs != 0;
+ }
+
+ return ret; /* not reached */
+}
+#endif /* CONFIG_CMD_MEMTEST */
+
+/* Modify memory.
+ *
+ * Syntax:
+ * mm {addr}
+ * nm {addr}
+ */
+static int
+mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
+{
+ uint32_t addr;
+ uint8_t data;
+ int nbytes;
+
+ (void) cmdtp;
+
+ if (argc != 2)
+ return CMD_RET_USAGE;
+
+ /* We use the last specified parameters, unless new ones are
+ * entered.
+ */
+ addr = mm_last_addr;
+
+ if ((flag & CMD_FLAG_REPEAT) == 0) {
+ /* New command specified.
+ */
+
+ /* Address is specified since argc > 1
+ */
+ addr = strtoul(argv[1], NULL, 16);
+ addr += base_address;
+ }
+
+ /* Print the address, followed by value. Then accept input for
+ * the next value. A non-converted value exits.
+ */
+ do {
+ z80_bus_cmd(Request);
+ data = z80_read(addr);
+ printf("%05lx: %02x", addr, data);
+ z80_bus_cmd(Release);
+
+ nbytes = cli_readline(PSTR(" ? "));
+ if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
+ /* <CR> pressed as only input, don't modify current
+ * location and move to next. "-" pressed will go back.
+ */
+ if (incrflag)
+ addr += nbytes ? -1 : 1;
+ nbytes = 1;
+ }
+ else {
+ char *endp;
+ data = strtoul(console_buffer, &endp, 16);
+ nbytes = endp - console_buffer;
+ if (nbytes) {
+ z80_bus_cmd(Request);
+ z80_write(addr, data);
+ z80_bus_cmd(Release);
+ if (incrflag)
+ addr++;
+ }
+ }
+ } while (nbytes);
+
+ mm_last_addr = addr;
+ return 0;
+}
+
diff --git a/avr/cmd_mem.h b/avr/cmd_mem.h
new file mode 100644
index 0000000..9803fd2
--- /dev/null
+++ b/avr/cmd_mem.h
@@ -0,0 +1,30 @@
+#ifndef CMD_MEM_H
+#define CMD_MEM_H
+
+//#include "common.h"
+
+#include "command.h"
+#include "cmd_mem.h"
+
+
+extern int do_mem_md(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_mm(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_nm(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_mw(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_cp(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_cmp(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_base(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_loop(cmd_tbl_t *, int, int, char * const []);
+#ifdef CONFIG_LOOPW
+extern int do_mem_loopw(cmd_tbl_t *, int, int, char * const []);
+#endif
+#ifdef CONFIG_CMD_MEMTEST
+extern int do_mem_mtest(cmd_tbl_t *, int, int, char * const []);
+#endif
+#ifdef CONFIG_MX_CYCLIC
+extern int do_mem_mdc(cmd_tbl_t *, int, int, char * const []);
+extern int do_mem_mwc(cmd_tbl_t *, int, int, char * const []);
+#endif /* CONFIG_MX_CYCLIC */
+
+#endif /* CMD_MEM_H */
+
diff --git a/avr/command.c b/avr/command.c
new file mode 100644
index 0000000..df4bb98
--- /dev/null
+++ b/avr/command.c
@@ -0,0 +1,526 @@
+
+/*
+ * Command Processor Table
+ */
+
+#include "common.h"
+
+#include <string.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "config.h"
+#include "debug.h"
+#include "con-utils.h"
+#include "env.h"
+#include "timer.h"
+#include "command.h"
+
+
+static void print_blanks(int_fast8_t count)
+{
+ while(count--)
+ my_puts_P(PSTR(" "));
+}
+
+static void print_usage_line(const FLASH char *name, const FLASH char *usage)
+{
+ my_puts_P(name);
+ print_blanks(CONFIG_SYS_MAXARGS - strlen_P(name));
+ my_puts_P(PSTR(" - "));
+ my_puts_P(usage);
+ my_puts_P(PSTR("\n"));
+}
+
+int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
+{
+ unsigned char c1, c2;
+
+ while ((c1 = *(const FLASH unsigned char *)s1++)
+ == (c2 = *(const FLASH unsigned char *)s2++))
+ if (c1 == 0)
+ return 0;
+
+ return c1 - c2;
+}
+
+int cmpstringp(const void *p1, const void *p2)
+{
+ return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name,
+ (*(const FLASH cmd_tbl_t **) p2)->name);
+}
+
+int cmd_tbl_item_count(void)
+{
+ cmd_tbl_t * p = cmd_tbl;
+ int count = 0;
+
+ while (p->name != NULL) {
+ p++; count++;
+ }
+ return count;
+}
+
+/*
+ * Use puts() instead of printf() to avoid printf buffer overflow
+ * for long help messages
+ */
+
+int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
+ int flag, int argc, char * const argv[])
+{
+ uint_fast8_t i;
+ int rcode = 0;
+
+ (void) flag;
+
+ if (argc == 1) { /*show list of commands */
+ cmd_tbl_t *cmd_array[cmd_items];
+ int i;
+
+ /* Make array of commands from .uboot_cmd section */
+ cmdtp = cmd_start;
+ for (i = 0; i < cmd_items; i++) {
+ cmd_array[i] = cmdtp++;
+ }
+
+ /* Sort command list */
+ qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstringp);
+
+ /* print short help (usage) */
+ for (i = 0; i < cmd_items; i++) {
+ const FLASH char *usage = cmd_array[i]->usage;
+
+ /* allow user abort */
+ if (ctrlc ())
+ return 1;
+ if (usage == NULL)
+ continue;
+#ifdef GCC_BUG_61443
+ print_usage_line(cmd_array[i]->name, usage);
+#else
+ printf_P(PSTR("%-" stringify(CONFIG_SYS_MAXARGS) "S - %S\n"),
+ cmd_array[i]->name, usage);
+#endif
+ }
+ return 0;
+ }
+ /*
+ * command help (long version)
+ */
+ for (i = 1; i < argc; ++i) {
+ if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
+ rcode |= cmd_usage(cmdtp);
+ } else {
+ printf_P(PSTR("Unknown command '%s' - try 'help'"
+ " without arguments.\n\n"), argv[i]
+ );
+ rcode = 1;
+ }
+ }
+ return rcode;
+}
+
+/***************************************************************************
+ * find command table entry for a command
+ */
+cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
+{
+ cmd_tbl_t *cmdtp;
+ cmd_tbl_t *cmdtp_temp = table; /*Init value */
+ size_t len;
+ uint_fast8_t n_found = 0;
+
+ if (!cmd)
+ return NULL;
+
+ len = strlen(cmd);
+
+ for (cmdtp = table;
+ cmdtp != table + table_len;
+ cmdtp++) {
+ if (strncmp_P (cmd, cmdtp->name, len) == 0) {
+ if (len == strlen (cmdtp->name))
+ return cmdtp; /* full match */
+
+ cmdtp_temp = cmdtp; /* abbreviated command ? */
+ n_found++;
+ }
+ }
+ if (n_found == 1) { /* exactly one match */
+ return cmdtp_temp;
+ }
+
+ return NULL; /* not found or ambiguous command */
+}
+
+
+cmd_tbl_t *find_cmd (const char *cmd)
+{
+ return find_cmd_tbl(cmd, cmd_tbl, cmd_tbl_item_count());
+}
+
+
+int cmd_usage(const FLASH cmd_tbl_t *cmdtp)
+{
+// printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
+ print_usage_line(cmdtp->name, cmdtp->usage);
+#if 0
+ my_puts_P(cmdtp->name);
+ print_blanks(CONFIG_SYS_MAXARGS - strlen_P(cmdtp->name));
+ my_puts_P(PSTR(" - "));
+ my_puts_P(cmdtp->usage);
+ my_puts_P(PSTR("\n\n"));
+#endif
+#ifdef CONFIG_SYS_LONGHELP
+// printf("Usage:\n%s ", cmdtp->name);
+ my_puts_P(PSTR("Usage:\n"));
+ my_puts_P(cmdtp->name);
+ my_puts_P(PSTR(" "));
+
+ if (!cmdtp->help) {
+ my_puts_P(PSTR(" - No additional help available.\n"));
+ return 1;
+ }
+
+ my_puts_P(cmdtp->help);
+ my_puts_P(PSTR("\n"));
+#endif /* CONFIG_SYS_LONGHELP */
+ return 1;
+}
+
+#ifdef CONFIG_AUTO_COMPLETE
+
+int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
+{
+ static char tmp_buf[512];
+ int space;
+
+ space = last_char == '\0' || isblank(last_char);
+
+ if (space && argc == 1)
+ return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
+
+ if (!space && argc == 2)
+ return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
+
+ return 0;
+}
+
+/*************************************************************************************/
+
+static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
+{
+ cmd_tbl_t *cmdtp = cmd_tbl;
+// const int count = ARRAY_SIZE(cmd_tbl);
+// const cmd_tbl_t *cmdend = cmdtp + count;
+// const char *p;
+ int len, clen;
+ int n_found = 0;
+ const char *cmd;
+
+ /* sanity? */
+ if (maxv < 2)
+ return -2;
+
+ cmdv[0] = NULL;
+
+ if (argc == 0) {
+ /* output full list of commands */
+ for (; cmdtp->name[0] != '\0'; cmdtp++) {
+ if (n_found >= maxv - 2) {
+ cmdv[n_found++] = "...";
+ break;
+ }
+ cmdv[n_found++] = cmdtp->name;
+ }
+ cmdv[n_found] = NULL;
+ return n_found;
+ }
+
+ /* more than one arg or one but the start of the next */
+ if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
+ cmdtp = find_cmd(argv[0]);
+ if (cmdtp == NULL || cmdtp->complete == NULL) {
+ cmdv[0] = NULL;
+ return 0;
+ }
+ return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
+ }
+
+ cmd = argv[0];
+
+ len = strlen(cmd);
+
+ /* return the partial matches */
+ for (; cmdtp->name[0] != '\0'; cmdtp++) {
+
+ clen = strlen(cmdtp->name);
+ if (clen < len)
+ continue;
+
+ if (memcmp(cmd, cmdtp->name, len) != 0)
+ continue;
+
+ /* too many! */
+ if (n_found >= maxv - 2) {
+ cmdv[n_found++] = "...";
+ break;
+ }
+
+ cmdv[n_found++] = cmdtp->name;
+ }
+
+ cmdv[n_found] = NULL;
+ return n_found;
+}
+
+static int make_argv(char *s, int argvsz, char *argv[])
+{
+ int argc = 0;
+
+ /* split into argv */
+ while (argc < argvsz - 1) {
+
+ /* skip any white space */
+ while (isblank(*s))
+ ++s;
+
+ if (*s == '\0') /* end of s, no more args */
+ break;
+
+ argv[argc++] = s; /* begin of argument string */
+
+ /* find end of string */
+ while (*s && !isblank(*s))
+ ++s;
+
+ if (*s == '\0') /* end of s, no more args */
+ break;
+
+ *s++ = '\0'; /* terminate current arg */
+ }
+ argv[argc] = NULL;
+
+ return argc;
+}
+
+static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
+{
+ int ll = leader != NULL ? strlen(leader) : 0;
+ int sl = sep != NULL ? strlen(sep) : 0;
+ int len, i;
+
+ if (banner) {
+ my_puts_P(PSTR("\n"));
+ my_puts(banner);
+ }
+
+ i = linemax; /* force leader and newline */
+ while (*argv != NULL) {
+ len = strlen(*argv) + sl;
+ if (i + len >= linemax) {
+ my_puts_P(PSTR("\n"));
+ if (leader)
+ my_puts(leader);
+ i = ll - sl;
+ } else if (sep)
+ my_puts(sep);
+ my_puts(*argv++);
+ i += len;
+ }
+ my_puts_P(PSTR("\n"));
+}
+
+static int find_common_prefix(char * const argv[])
+{
+ int i, len;
+ char *anchor, *s, *t;
+
+ if (*argv == NULL)
+ return 0;
+
+ /* begin with max */
+ anchor = *argv++;
+ len = strlen(anchor);
+ while ((t = *argv++) != NULL) {
+ s = anchor;
+ for (i = 0; i < len; i++, t++, s++) {
+ if (*t != *s)
+ break;
+ }
+ len = s - anchor;
+ }
+ return len;
+}
+
+static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
+
+int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp)
+{
+ int n = *np, col = *colp;
+ char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
+ char *cmdv[20];
+ char *s, *t;
+ const char *sep;
+ int i, j, k, len, seplen, argc;
+ int cnt;
+ char last_char;
+
+ if (strcmp_PP(prompt, CONFIG_SYS_PROMPT) != 0)
+ return 0; /* not in normal console */
+
+ cnt = strlen(buf);
+ if (cnt >= 1)
+ last_char = buf[cnt - 1];
+ else
+ last_char = '\0';
+
+ /* copy to secondary buffer which will be affected */
+ strcpy(tmp_buf, buf);
+
+ /* separate into argv */
+ argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
+
+ /* do the completion and return the possible completions */
+ i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
+
+ /* no match; bell and out */
+ if (i == 0) {
+ if (argc > 1) /* allow tab for non command */
+ return 0;
+ putchar('\a');
+ return 1;
+ }
+
+ s = NULL;
+ len = 0;
+ sep = NULL;
+ seplen = 0;
+ if (i == 1) { /* one match; perfect */
+ k = strlen(argv[argc - 1]);
+ s = cmdv[0] + k;
+ len = strlen(s);
+ sep = " ";
+ seplen = 1;
+ } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
+ k = strlen(argv[argc - 1]);
+ j -= k;
+ if (j > 0) {
+ s = cmdv[0] + k;
+ len = j;
+ }
+ }
+
+ if (s != NULL) {
+ k = len + seplen;
+ /* make sure it fits */
+ if (n + k >= CONFIG_SYS_CBSIZE - 2) {
+ putchar('\a');
+ return 1;
+ }
+
+ t = buf + cnt;
+ for (i = 0; i < len; i++)
+ *t++ = *s++;
+ if (sep != NULL)
+ for (i = 0; i < seplen; i++)
+ *t++ = sep[i];
+ *t = '\0';
+ n += k;
+ col += k;
+ my_puts(t - k);
+ if (sep == NULL)
+ putchar('\a');
+ *np = n;
+ *colp = col;
+ } else {
+ print_argv(NULL, " ", " ", 78, cmdv);
+
+ my_puts_P(prompt);
+ my_puts(buf);
+ }
+ return 1;
+}
+
+#endif /* CONFIG_AUTO_COMPLETE */
+
+
+
+/**
+ * Call a command function. This should be the only route in U-Boot to call
+ * a command, so that we can track whether we are waiting for input or
+ * executing a command.
+ *
+ * @param cmdtp Pointer to the command to execute
+ * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
+ * @param argc Number of arguments (arg 0 must be the command text)
+ * @param argv Arguments
+ * @return 0 if command succeeded, else non-zero (CMD_RET_...)
+ */
+static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ int result;
+
+ result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
+ if (result)
+ debug("Command failed, result=%d\n", result);
+ return result;
+}
+
+enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
+ uint_fast8_t *repeatable)
+{
+ enum command_ret_t rc = CMD_RET_SUCCESS;
+ cmd_tbl_t *cmdtp;
+
+ /* Look up command in command table */
+ cmdtp = find_cmd(argv[0]);
+ if (cmdtp == NULL) {
+ printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);
+ return 1;
+ }
+ if (!cmdtp->cmd) {
+ debug("### Command '%s' found, but ->cmd == NULL \n", argv[0]);
+ return 1;
+ }
+
+ /* found - check max args */
+ if (argc > cmdtp->maxargs)
+ rc = CMD_RET_USAGE;
+
+#if defined(CONFIG_CMD_BOOTD)
+ /* avoid "bootd" recursion */
+ else if (cmdtp->cmd == do_bootd) {
+ if (flag & CMD_FLAG_BOOTD) {
+ my_puts_P(PSTR("'bootd' recursion detected\n"));
+ rc = CMD_RET_FAILURE;
+ } else {
+ flag |= CMD_FLAG_BOOTD;
+ }
+ }
+#endif
+
+ /* If OK so far, then do the command */
+ if (!rc) {
+ rc = cmd_call(cmdtp, flag, argc, argv);
+ *repeatable &= cmdtp->repeatable;
+ }
+ if (rc == CMD_RET_USAGE)
+ rc = cmd_usage(cmdtp);
+ return rc;
+}
+
+int cmd_process_error(cmd_tbl_t *cmdtp, int err)
+{
+ char buf[strlen_P(cmdtp->name) + 1];
+ strcpy_P(buf, cmdtp->name);
+
+ if (err) {
+ printf_P(PSTR("Command '%s' failed: Error %d\n"), buf, err);
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/avr/command.h b/avr/command.h
new file mode 100644
index 0000000..82b83ba
--- /dev/null
+++ b/avr/command.h
@@ -0,0 +1,157 @@
+
+/*
+ * Definitions for Command Processor
+ */
+#ifndef __COMMAND_H
+#define __COMMAND_H
+
+#include "common.h"
+#include "config.h"
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+/* Default to a width of 8 characters for help message command width */
+#ifndef CONFIG_SYS_HELP_CMD_WIDTH
+#define CONFIG_SYS_HELP_CMD_WIDTH 8
+#endif
+
+/*
+ * Monitor Command Table
+ */
+
+struct cmd_tbl_s {
+ const FLASH char *name; /* Command Name */
+ int maxargs; /* maximum number of arguments */
+ int repeatable; /* autorepeat allowed? */
+ /* Implementation function */
+ int (*cmd)(const FLASH struct cmd_tbl_s *, int, int, char * const []);
+ const FLASH char *usage; /* Usage message (short) */
+#ifdef CONFIG_SYS_LONGHELP
+ const FLASH char *help; /* Help message (long) */
+#endif
+#ifdef CONFIG_AUTO_COMPLETE
+ /* do auto completion on the arguments */
+ int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
+#endif
+};
+
+typedef const FLASH struct cmd_tbl_s cmd_tbl_t;
+
+extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+
+/* command.c */
+int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
+ flag, int argc, char * const argv[]);
+cmd_tbl_t *find_cmd(const char *cmd);
+cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
+
+int cmd_tbl_item_count(void);
+int cmd_usage(cmd_tbl_t *cmdtp);
+
+#ifdef CONFIG_AUTO_COMPLETE
+extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
+extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
+#endif
+
+/**
+ * cmd_process_error() - report and process a possible error
+ *
+ * @cmdtp: Command which caused the error
+ * @err: Error code (0 if none, -ve for error, like -EIO)
+ * @return 0 if there is not error, 1 (CMD_RET_FAILURE) if an error is found
+ */
+int cmd_process_error(cmd_tbl_t *cmdtp, int err);
+
+/*
+ * Monitor Command
+ *
+ * All commands use a common argument format:
+ *
+ * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+ */
+
+
+#ifdef CONFIG_CMD_BOOTD
+extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+#endif
+#ifdef CONFIG_CMD_BOOTM
+extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
+#else
+static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
+{
+ (void) cmdtp; (void) cmd;
+
+ return 0;
+}
+#endif
+
+extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
+ char *const argv[]);
+
+extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+
+/*
+ * Error codes that commands return to cmd_process(). We use the standard 0
+ * and 1 for success and failure, but add one more case - failure with a
+ * request to call cmd_usage(). But the cmd_process() function handles
+ * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
+ * This is just a convenience for commands to avoid them having to call
+ * cmd_usage() all over the place.
+ */
+enum command_ret_t {
+ CMD_RET_SUCCESS, /* 0 = Success */
+ CMD_RET_FAILURE, /* 1 = Failure */
+ CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
+};
+
+/**
+ * Process a command with arguments. We look up the command and execute it
+ * if valid. Otherwise we print a usage message.
+ *
+ * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
+ * @param argc Number of arguments (arg 0 must be the command text)
+ * @param argv Arguments
+ * @param repeatable This function sets this to 0 if the command is not
+ * repeatable. If the command is repeatable, the value
+ * is left unchanged.
+ * @return 0 if the command succeeded, 1 if it failed
+ */
+int cmd_process(int flag, int argc, char * const argv[], uint_fast8_t *repeatable);
+
+
+/*
+ * Command Flags:
+ */
+#define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
+#define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
+
+#ifdef CONFIG_AUTO_COMPLETE
+# define _CMD_COMPLETE(x) x,
+#else
+# define _CMD_COMPLETE(x)
+#endif
+#ifdef CONFIG_SYS_LONGHELP
+# define _CMD_HELP(x) x,
+#else
+# define _CMD_HELP(x)
+#endif
+
+
+#define CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
+ _usage, _help, _comp) \
+ { FSTR(#_name), _maxargs, _rep, _cmd, FSTR(_usage), \
+ _CMD_HELP(FSTR(_help)) _CMD_COMPLETE(_comp) }
+
+#define CMD_TBL_ITEM(_name, _maxargs, _rep, _cmd, _usage, _help) \
+ CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
+ _usage, _help, NULL)
+
+typedef int (*do_cmd_t)(cmd_tbl_t *, int, int, char * const []);
+
+extern cmd_tbl_t cmd_tbl[];
+
+
+#endif /* __COMMAND_H */
diff --git a/avr/command_tbl.c b/avr/command_tbl.c
new file mode 100644
index 0000000..69f6273
--- /dev/null
+++ b/avr/command_tbl.c
@@ -0,0 +1,201 @@
+
+#include "common.h"
+
+#include "command.h"
+#include "cmd_mem.h"
+
+
+extern int do_help(cmd_tbl_t *, int, int, char * const []);
+extern int do_echo(cmd_tbl_t *, int, int, char * const []);
+extern int do_env_print(cmd_tbl_t *, int, int, char * const []);
+extern int do_env_set(cmd_tbl_t *, int, int, char * const []);
+extern int do_env_save(cmd_tbl_t *, int, int, char * const []);
+extern int do_loadf(cmd_tbl_t *, int, int, char * const []);
+extern int do_go(cmd_tbl_t *, int, int, char * const []);
+extern int do_restart(cmd_tbl_t *, int, int, char * const []);
+extern int do_dump_mem(cmd_tbl_t *, int, int, char * const []);
+extern int do_eep_cp(cmd_tbl_t *, int, int, char * const []);
+extern int do_busreq_pulse(cmd_tbl_t *, int, int, char * const []);
+
+
+cmd_tbl_t cmd_tbl[] = {
+
+#ifdef DEBUG
+CMD_TBL_ITEM(
+ !mdr, 3, 1, do_dump_mem,
+ "RAM dump",
+ "address [count]"
+),
+CMD_TBL_ITEM(
+ !mde, 3, 1, do_dump_mem,
+ "EEPROM dump",
+ "address [count]"
+),
+CMD_TBL_ITEM(
+ !cpe, 4, 0, do_eep_cp,
+ "EEPROM copy",
+ "source target count"
+),
+#endif
+CMD_TBL_ITEM(
+ mstep, 2, 1, do_busreq_pulse,
+ "execute one M cycle",
+ "[count]\n"
+ " - repeat count times"
+),
+CMD_TBL_ITEM(
+ echo, CONFIG_SYS_MAXARGS, 1, do_echo,
+ "echo args to console",
+ "[args..]\n"
+ " - echo args to console; \\c suppresses newline"
+),
+CMD_TBL_ITEM_COMPLETE(
+ run, CONFIG_SYS_MAXARGS, 1, do_run,
+ "run commands in an environment variable",
+ "var [...]\n"
+ " - run the commands in the environment variable(s) 'var'",
+ var_complete
+),
+CMD_TBL_ITEM_COMPLETE(
+ printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
+ "print environment variables",
+ "\n"
+ " - print values of all environment variables\n"
+ "printenv name ...\n"
+ " - print value of environment variable 'name'",
+ var_complete
+),
+CMD_TBL_ITEM_COMPLETE(
+ setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
+ "set environment variables",
+ "name value ...\n"
+ " - set environment variable 'name' to 'value ...'\n"
+ "setenv name\n"
+ " - delete environment variable 'name'",
+ var_complete
+),
+CMD_TBL_ITEM(
+ saveenv, 1, 0, do_env_save,
+ "save environment variables to persistent storage",
+ ""
+),
+
+CMD_TBL_ITEM(
+ loadf, 1, 0, do_loadf,
+ "load srec_cat prepared image from controller flash",
+ ""
+),
+CMD_TBL_ITEM(
+ go, 2, 0, do_go,
+ "start application at address 'addr'",
+ "addr\n"
+ " - start application at address 'addr'"
+// "\n"
+// " passing 'arg' as arguments"
+),
+CMD_TBL_ITEM(
+ reset, 1, 0, do_reset,
+ "Keep CPU in RESET state",
+ ""
+),
+CMD_TBL_ITEM(
+ restart, 1, 0, do_restart,
+ "Perform RESET of the CPU",
+ ""
+),
+
+CMD_TBL_ITEM(
+ md, 3, 1, do_mem_md,
+ "memory display",
+ "address [# of objects]"
+),
+CMD_TBL_ITEM(
+ mm, 2, 1, do_mem_mm,
+ "memory modify (auto-incrementing address)",
+ "address"
+),
+CMD_TBL_ITEM(
+ nm, 2, 1, do_mem_nm,
+ "memory modify (constant address)",
+ "address"
+),
+CMD_TBL_ITEM(
+ mw, 4, 1, do_mem_mw,
+ "memory write (fill)",
+ "address value [count]"
+),
+CMD_TBL_ITEM(
+ cp, 4, 1, do_mem_cp,
+ "memory copy",
+ "source target count"
+),
+CMD_TBL_ITEM(
+ cmp, 4, 1, do_mem_cmp,
+ "memory compare",
+ "addr1 addr2 count"
+),
+CMD_TBL_ITEM(
+ base, 2, 1, do_mem_base,
+ "print or set address offset",
+ "\n"
+ " - print address offset for memory commands\n"
+ "base offset\n"
+ " - set address offset for memory commands to 'offset'"
+),
+CMD_TBL_ITEM(
+ loop, 3, 1, do_mem_loop,
+ "infinite loop on address range",
+ "address number_of_bytes"
+),
+#ifdef CONFIG_LOOPW
+CMD_TBL_ITEM(
+ loopw, 4, 1, do_mem_loopw,
+ "infinite write loop on address range",
+ "address number_of_bytes data_to_write"
+),
+#endif /* CONFIG_LOOPW */
+
+#ifdef CONFIG_CMD_MEMTEST
+CMD_TBL_ITEM(
+ mtest, 5, 1, do_mem_mtest,
+ "simple RAM read/write test",
+ "[start [end [pattern [iterations]]]]"
+),
+#endif /* CONFIG_CMD_MEMTEST */
+
+#ifdef CONFIG_MX_CYCLIC
+CMD_TBL_ITEM(
+ mdc, 4, 1, do_mem_mdc,
+ "memory display cyclic",
+ "address count delay(ms)"
+),
+CMD_TBL_ITEM(
+ mwc, 4, 1, do_mem_mwc,
+ "memory write cyclic",
+ "address value delay(ms)"
+),
+#endif /* CONFIG_MX_CYCLIC */
+
+
+CMD_TBL_ITEM(
+ help, CONFIG_SYS_MAXARGS, 1, do_help,
+ "print command description/usage",
+ "\n"
+ " - print brief description of all commands\n"
+ "help command ...\n"
+ " - print detailed usage of 'command'"
+),
+
+/* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
+ {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help,
+ FSTR("alias for 'help'"),
+#ifdef CONFIG_SYS_LONGHELP
+ FSTR(""),
+#endif /* CONFIG_SYS_LONGHELP */
+#ifdef CONFIG_AUTO_COMPLETE
+ 0,
+#endif
+},
+/* Mark end of table */
+{ 0 },
+};
diff --git a/avr/common.h b/avr/common.h
new file mode 100644
index 0000000..a92f62c
--- /dev/null
+++ b/avr/common.h
@@ -0,0 +1,61 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#ifdef __AVR__
+#include <avr/io.h>
+
+//TODO:
+// Known to work: 4.8.4, 4.9.1
+// Known to fail: 4.8.3, 4.9.0
+#define GCC_BUG_61443 1
+
+#else
+// TODO: stm32
+#endif /* __AVR__ */
+
+#include <stdio.h>
+
+#ifdef __FLASH
+#define FLASH __flash
+#else
+#define FLASH
+#endif
+
+#define stringify(s) tostring(s)
+#define tostring(s) #s
+
+#define FSTR(X) ((const FLASH char[]) { X } )
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+
+#ifdef __AVR__
+#define Stat GPIOR0
+#else
+extern volatile uint_least8_t Stat;
+#endif /* __AVR__ */
+
+#define S_10MS_TO (1<<0)
+#define S_Z180_RUNNING (2<<0)
+
+static inline
+void my_puts(const char *s)
+{
+ fputs(s, stdout);
+}
+
+#ifdef __AVR__
+static inline
+void my_puts_P(const char *s)
+{
+ fputs_P(s, stdout);
+}
+
+#else
+static inline
+void my_puts_P(const char *s)
+{
+ fputs(s, stdout);
+}
+#endif /* __AVR__ */
+#endif /* COMMON_H */
+
diff --git a/avr/con-utils.c b/avr/con-utils.c
new file mode 100644
index 0000000..02657b9
--- /dev/null
+++ b/avr/con-utils.c
@@ -0,0 +1,95 @@
+
+#include <string.h>
+#include "common.h"
+
+#include "serial.h"
+#include "background.h"
+#include "con-utils.h"
+
+uint_fast8_t tstc(void)
+{
+ bg_shed();
+ return serial_tstc();
+}
+
+int my_getchar(void)
+{
+ int c;
+
+ do {
+ bg_shed();
+ c = serial_getc();
+ } while (c < 0);
+
+ return c;
+}
+
+
+/* test if ctrl-c was pressed */
+
+static uint_fast8_t ctrlc_disabled; /* see disable_ctrl() */
+static uint_fast8_t ctrlc_was_pressed;
+
+uint_fast8_t ctrlc(void)
+{
+ if (!ctrlc_disabled) {
+ switch (serial_getc()) {
+ case 0x03: /* ^C - Control C */
+ ctrlc_was_pressed = 1;
+ return 1;
+ default:
+ break;
+ }
+ }
+ return 0;
+}
+
+/* Reads user's confirmation.
+ Returns 1 if user's input is "y", "Y", "yes" or "YES"
+*/
+uint_fast8_t confirm_yesno(void)
+{
+ unsigned int i;
+ char str_input[5];
+
+ /* Flush input */
+ while (serial_getc())
+ ;
+ i = 0;
+ while (i < sizeof(str_input)) {
+ str_input[i] = my_getchar();
+ putchar(str_input[i]);
+ if (str_input[i] == '\r')
+ break;
+ i++;
+ }
+ putchar('\n');
+ if (strncmp(str_input, "y\r", 2) == 0 ||
+ strncmp(str_input, "Y\r", 2) == 0 ||
+ strncmp(str_input, "yes\r", 4) == 0 ||
+ strncmp(str_input, "YES\r", 4) == 0)
+ return 1;
+ return 0;
+}
+
+/* pass 1 to disable ctrlc() checking, 0 to enable.
+ * returns previous state
+ */
+uint_fast8_t disable_ctrlc(uint_fast8_t disable)
+{
+ uint_fast8_t prev = ctrlc_disabled; /* save previous state */
+
+ ctrlc_disabled = disable;
+ return prev;
+}
+
+uint_fast8_t had_ctrlc (void)
+{
+ return ctrlc_was_pressed;
+}
+
+void clear_ctrlc(void)
+{
+ ctrlc_was_pressed = 0;
+}
+
diff --git a/avr/con-utils.h b/avr/con-utils.h
new file mode 100644
index 0000000..f03dfb3
--- /dev/null
+++ b/avr/con-utils.h
@@ -0,0 +1,22 @@
+#ifndef CON_UTILS_H
+#define CON_UTILS_H
+
+uint_fast8_t tstc(void);
+
+int my_getchar(void);
+
+/* test if ctrl-c was pressed */
+uint_fast8_t ctrlc(void);
+
+
+/* pass 1 to disable ctrlc() checking, 0 to enable.
+ * returns previous state
+ */
+uint_fast8_t disable_ctrlc(uint_fast8_t disable);
+
+uint_fast8_t had_ctrlc (void);
+void clear_ctrlc(void);
+
+#endif /* CON_UTILS_H */
+
+
diff --git a/avr/config.h b/avr/config.h
new file mode 100644
index 0000000..13de63b
--- /dev/null
+++ b/avr/config.h
@@ -0,0 +1,28 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#define CONFIG_ENV_SIZE 1600
+#define CONFIG_ENV_OFFSET 0
+#define CONFIG_ENVVAR_MAX 20
+
+#define CONFIG_BOOTDELAY 4
+//#define CONFIG_ZERO_BOOTDELAY_CHECK 1
+
+//#define CONFIG_LOOPW
+//#define CONFIG_CMD_MEMTEST
+//#define CONFIG_MX_CYCLIC
+
+#define CONFIG_SYS_CBSIZE 250
+#define CONFIG_SYS_ENV_NAMELEN 16
+#define CONFIG_SYS_MAXARGS 8
+
+#define CONFIG_SYS_PROMPT "=> "
+
+
+//#define CONFIG_CMDLINE_EDITING 1
+//#define CONFIG_AUTO_COMPLETE 1
+
+#define CONFIG_SYS_LONGHELP 1
+
+#endif /* CONFIG_H */
+
diff --git a/avr/crc.h b/avr/crc.h
new file mode 100644
index 0000000..c38bae4
--- /dev/null
+++ b/avr/crc.h
@@ -0,0 +1,12 @@
+#ifndef CRC_H
+#define CRC_H
+
+#include <util/crc16.h>
+
+static inline
+uint16_t crc16(uint16_t crc, uint8_t data)
+{
+ return _crc_ccitt_update(crc, data);
+}
+
+#endif /* CRC_H */
diff --git a/avr/debug.c b/avr/debug.c
new file mode 100644
index 0000000..e62869b
--- /dev/null
+++ b/avr/debug.c
@@ -0,0 +1,227 @@
+#include "common.h"
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <avr/eeprom.h>
+
+#include "command.h"
+#include "debug.h"
+
+/*
+ * Debugging
+ */
+#ifdef DEBUG
+
+//uint8_t eeprom_read_byte (const uint8_t *__p)
+
+static void print_blanks(uint_fast8_t count)
+{
+ while(count--)
+ putchar(' ');
+}
+
+static uint8_t ram_read_byte(const uint8_t *p)
+{
+ return *p;
+}
+
+void dump_mem(const uint8_t *startaddr, int len,
+ uint8_t (*readfkt)(const uint8_t *), char *title)
+{
+ uint8_t buf[16];
+ uint8_t llen = 16;
+ uint8_t pre = (size_t) startaddr % 16;
+ const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
+ len += pre;
+ uint8_t i;
+
+ if (title && *title)
+ printf_P(PSTR("%s\n"),title);
+
+ while (len) {
+ if (len < 16)
+ llen = len;
+
+ for (i = pre; i < llen; i++)
+ buf[i] = readfkt(addr + i);
+
+ printf_P(PSTR("%04x:"), addr);
+ for (i = 0; i < llen; i++) {
+ if ((i % 8) == 0)
+ putchar(' ');
+ if (i < pre)
+ printf_P(PSTR(".. "));
+ else
+ printf_P(PSTR("%.2x "), buf[i]);
+ }
+ /* fill line with whitespace for nice ASCII print */
+ print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre);
+ /* Print data in ASCII characters */
+ for (i = pre; i < llen; i++)
+ printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
+ putchar('\n');
+
+ pre = 0;
+ addr += 16;
+ len -= llen;
+ }
+}
+
+#if 0
+void dump_ram(const uint8_t *startaddr, int len, char *title)
+{
+ uint8_t llen = 16;
+ uint8_t pre = (size_t) startaddr % 16;
+ const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
+ len += pre;
+ uint8_t i;
+
+ if (title && *title)
+ printf_P(PSTR("%s\n"),title);
+
+ while (len) {
+ if (len < 16)
+ llen = len;
+
+ printf_P(PSTR(" %.4x:"), (size_t) addr);
+ print_blanks(3 * pre);
+ for (i = pre; i < llen; i++)
+ printf_P(PSTR(" %.2x"), addr[i]);
+ print_blanks(3 * (16 - i + 1) + pre);
+ for (i = pre; i < llen; i++)
+ printf_P(PSTR("%c"), isprint(addr[i]) ? addr[i] : '.');
+ putchar('\n');
+
+ pre = 0;
+ addr += 16;
+ len -= llen;
+ }
+}
+#endif
+#if 0
+void dump_heap(void)
+{
+ extern unsigned int __brkval;
+
+ dump_ram((uint8_t *) __malloc_heap_start,
+ __brkval - (unsigned int) __malloc_heap_start,
+ "=== Heap:");
+}
+#endif
+
+#if 0
+/* TODO: combine with dump_ram() */
+void dump_eep(const uint8_t *addr, unsigned int len,
+ uint8_t (*readfkt)(const uint8_t *))
+{
+ uint_fast8_t i;
+ uint8_t buf[16];
+
+ printf_P(PSTR("eeprom dump:"));
+ while (len) {
+ printf_P(PSTR("\n 0x%.4x:"), (unsigned int) addr);
+ for (i = 0; i<16; i++)
+ buf[i] = readfkt(addr + i);
+ for (i = 0; i<16; i++)
+ printf_P(PSTR(" %.2x"), buf[i]);
+ printf_P(PSTR(" "));
+ for (i = 0; i<16; i++)
+ printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
+
+ addr += 16;
+ len -= len > 16 ? 16 : len;
+ }
+ putchar('\n');
+}
+#endif
+
+
+/*
+ * EEPROM Display
+ * md addr {len}
+ */
+int do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+// static const uint8_t *addr;
+// static uint16_t length = 128;
+ uint8_t (*readhow)(const uint8_t *);
+
+ (void) cmdtp; (void) flag;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+ const uint8_t *addr;
+ uint16_t length = 128;
+
+ if (strchr(argv[0],'r') != NULL)
+ readhow = ram_read_byte;
+ else if (strchr(argv[0],'e') != NULL)
+ readhow = eeprom_read_byte;
+ else
+ return CMD_RET_USAGE;
+
+ /* Address is specified since argc > 1 */
+ addr = (const uint8_t *) (size_t) strtoul(argv[1], NULL, 16);
+
+ /* If another parameter, it is the length to display. */
+ if (argc > 2)
+ length = (uint16_t) strtoul(argv[2], NULL, 16);
+
+ /* Print the lines. */
+ dump_mem(addr, length, readhow, NULL);
+
+ return 0;
+}
+
+int do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ uint16_t src, dest, count;
+ int_fast8_t step;
+
+ (void) cmdtp;
+ (void) flag;
+
+ if (argc != 4)
+ return CMD_RET_USAGE;
+
+ src = (size_t) strtoul(argv[1], NULL, 16);
+ dest = (size_t) strtoul(argv[2], NULL, 16);
+ count = (size_t) strtoul(argv[3], NULL, 16);
+
+ if (src > E2END) {
+ debug("src > EEPROM size: 0x%04x\n", src);
+ return 1;
+ }
+ if (dest > E2END) {
+ debug("dest > EEPROM size: 0x%04x\n", dest);
+ return 1;
+ }
+ if (count > E2END+1) {
+ debug("count > EEPROM size: 0x%04x\n", count);
+ return 1;
+ }
+ if (count == 0) {
+ debug("Zero length ???\n");
+ return 1;
+ }
+
+ if (dest > src) {
+ src += count - 1;
+ dest += count - 1;
+ step = -1;
+ } else
+ step = 1;
+
+ while (count-- > 0) {
+ uint8_t data;
+ data = eeprom_read_byte((uint8_t *) src);
+ eeprom_write_byte((uint8_t *) dest, data);
+ src += step;
+ dest += step;
+
+ }
+ return 0;
+}
+#endif /* DEBUG */
+
diff --git a/avr/debug.h b/avr/debug.h
new file mode 100644
index 0000000..1815166
--- /dev/null
+++ b/avr/debug.h
@@ -0,0 +1,34 @@
+
+#ifndef DEBUG_H_
+#define DEBUG_H_
+
+#include "common.h"
+#include <avr/pgmspace.h>
+
+#ifdef DEBUG
+#define _DEBUG 1
+#else
+#define _DEBUG 0
+#endif
+
+#define debug_cond(cond, fmt, args...) \
+ do { \
+ if (cond) \
+ printf_P(PSTR(fmt), ##args); \
+ } while (0)
+
+#define debug(fmt, args...) \
+ debug_cond(_DEBUG, fmt, ##args)
+
+
+#if 1
+#ifdef DEBUG
+#define DBG_P(lvl, format, ...) if (DEBUG>=lvl) \
+ fprintf_P( stdout, PSTR(format), ##__VA_ARGS__ )
+#else
+#define DBG_P(lvl, ...)
+#endif
+#endif /* 0 */
+
+#endif /* DEBUG_H_ */
+
diff --git a/avr/env.c b/avr/env.c
new file mode 100644
index 0000000..7cf33ac
--- /dev/null
+++ b/avr/env.c
@@ -0,0 +1,833 @@
+#include "common.h"
+#include <string.h>
+#include <stdlib.h>
+
+#include <avr/eeprom.h>
+
+#include "config.h"
+#include "debug.h"
+#include "xmalloc.h"
+#include "crc.h"
+#include "command.h"
+
+#include "env.h"
+
+
+#define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
+#define ACTIVE_FLAG 1
+#define OBSOLETE_FLAG 0
+#define ENV_GET_VAL (1<<0)
+
+
+/*
+ * Default Environment
+ */
+
+#define DELIM "\0"
+
+const FLASH char default_env[] = {
+ "bootdelay=" "3" DELIM
+ "bootcmd=" "reset; loadf; go $(startaddr)" DELIM
+ "baudrate=" "115200" DELIM
+ "startaddr=" "0" DELIM
+ DELIM
+};
+
+/* EEPROM storage */
+typedef struct environment_s {
+ uint16_t crc; /* CRC16 over data bytes */
+ uint8_t flags; /* active/obsolete flags */
+ char data[ENV_SIZE]; /* Environment data */
+} env_t;
+
+
+/* */
+typedef struct env_item_s {
+#define EF_N_EEP (1<<7) /* Variable name is in EEPROM */
+#define EF_V_EEP (1<<6) /* Variable value is in EEPROM */
+#define EF_DIRTY (1<<0) /* Variable is new or value changed */
+ uint8_t flags;
+ union {
+ uint16_t eep;
+ char *ram;
+ } name;
+ union {
+ uint16_t eep;
+ char *ram;
+ } val;
+} env_item_t;
+
+
+static uint8_t env_valid;
+static env_item_t env_list[CONFIG_ENVVAR_MAX];
+static int entrycount;
+
+
+static
+char env_get_char(uint16_t index)
+{
+ unsigned int off = CONFIG_ENV_OFFSET;
+
+ if (env_valid == 2)
+ off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
+
+ return (char) eeprom_read_byte((const uint8_t *)off + index +
+ offsetof(env_t, data));
+}
+
+
+static
+uint16_t varname_get(char *buf, env_item_t *ep)
+{
+ int i = 0;
+ char c;
+
+ if (ep->flags & EF_N_EEP) {
+
+ while ((c = env_get_char(ep->name.eep + i)) != '=' &&
+ c != '\0' && i < CONFIG_SYS_ENV_NAMELEN) {
+
+ buf[i] = c;
+ i++;
+ }
+ if (i > 0 && c != '=') {
+ debug("** ee_name: '%s' not '=' terminated!\n", buf);
+ }
+ } else {
+ strncpy(buf, ep->name.ram, CONFIG_SYS_ENV_NAMELEN);
+ i = strnlen(buf, CONFIG_SYS_ENV_NAMELEN);
+ }
+ buf[i] = '\0';
+ return i;
+}
+
+
+static
+uint16_t varval_get(char *buf, uint16_t index, int len)
+{
+ int i = 0;
+ char c;
+
+ while ((c = env_get_char (index + i)) != '\0' && i < len) {
+ buf[i] = c;
+ i++;
+ };
+
+ buf[i] = '\0';
+
+ /* TODO: len check */
+
+ return i;
+}
+
+static
+int comp_env_key_item(const void *key, const void *item)
+{
+ char buf[CONFIG_SYS_ENV_NAMELEN+1];
+ env_item_t *ep = (env_item_t *) item;
+
+ varname_get(buf, ep);
+
+ return strcmp((char *) key, buf);
+}
+
+static
+int comp_env_items(const void *m1, const void *m2)
+{
+ char b1[CONFIG_SYS_ENV_NAMELEN+1];
+ char b2[CONFIG_SYS_ENV_NAMELEN+1];
+
+ env_item_t *ep1 = (env_item_t *) m1;
+ env_item_t *ep2 = (env_item_t *) m2;
+
+ varname_get(b1, ep1);
+ varname_get(b2, ep2);
+
+ return strcmp(b1, b2);
+}
+
+
+static
+int envlist_import(void)
+{
+ char name[CONFIG_SYS_ENV_NAMELEN+1];
+ uint16_t idx = 0;
+ int nlen;
+ env_item_t e;
+
+ e.flags = EF_N_EEP | EF_V_EEP;
+ e.name.eep = idx;
+ while ((nlen = varname_get(name, &e)) != 0 && idx < ENV_SIZE) {
+
+ if (entrycount <= CONFIG_ENVVAR_MAX) {
+ e.val.eep = idx + nlen + 1;
+
+ env_list[entrycount++] = e;
+
+ idx += nlen + 1;
+ while (env_get_char(idx++) != 0 && idx < ENV_SIZE)
+ ;
+ e.name.eep = idx;
+ } else {
+ debug("** Too many environment variables!\n");
+ break;
+ }
+ }
+ qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items);
+
+ return 0;
+}
+
+
+static
+int set_default_env(void)
+{
+ char buf[56];
+ uint16_t eep = CONFIG_ENV_OFFSET + offsetof(env_t, data);
+ unsigned int len = ENV_SIZE;
+ unsigned int i, src = 0;
+ char c = 0xff, c0 = c;
+
+ if (env_valid == 1) {
+ eep = CONFIG_ENV_OFFSET + offsetof(env_t, data) + CONFIG_ENV_SIZE;
+ }
+
+ while (len) {
+ memcpy_P(buf, default_env+src, sizeof(buf));
+ for (i=0; i < (len < sizeof(buf) ? len : sizeof(buf)) &&
+ !(c == 0 && c0 == 0);
+ i++) {
+ c0 = c; c = buf[i];
+ }
+ eeprom_update_block(buf, (char *) eep, i);
+ if (c == 0 && c0 == 0)
+ len = 0;
+ if (len > sizeof(buf))
+ len -= sizeof(buf);
+ src += sizeof(buf);
+ eep += sizeof(buf);
+ }
+
+ return 0;
+}
+
+
+static
+uint16_t env_crc(uint16_t data_offset)
+{
+ uint16_t crc;
+ uint16_t i;
+ char c, c0;
+
+ crc = 0xffff;
+ c = 0xff;
+ for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++)
+ {
+ c0 = c;
+ c = eeprom_read_byte((const uint8_t *) data_offset + i);
+ crc = crc16(crc, c);
+ }
+ return crc ;
+}
+
+
+/**
+ * return valid env
+ */
+static
+int env_check_valid(void)
+{
+ const uint16_t offset[2] = {CONFIG_ENV_OFFSET,
+ CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE};
+ uint_fast8_t flags[2], crc_ok[2];
+ int rc;
+
+ /* read FLAGS */
+ flags[0] = eeprom_read_byte ((uint8_t *) offset[0] +
+ offsetof(env_t, flags));
+ flags[1] = eeprom_read_byte ((uint8_t *) offset[1] +
+ offsetof(env_t, flags));
+
+ /* check CRC */
+ crc_ok[0] = (
+ eeprom_read_word((uint16_t *) offset[0] +
+ offsetof(env_t, crc))
+ == env_crc(offset[0] + offsetof(env_t, data))
+ );
+ crc_ok[1] = (
+ eeprom_read_word((uint16_t *) offset[1] +
+ offsetof(env_t, crc))
+ == env_crc(offset[1] + offsetof(env_t, data))
+ );
+
+ if (!crc_ok[0] && !crc_ok[1]) {
+ rc = 0;
+
+ } else if (crc_ok[0] && !crc_ok[1]) {
+ rc = 1;
+ } else if (!crc_ok[0] && crc_ok[1]) {
+ rc = 2;
+ } else {
+ /* both ok - check serial */
+#if 1
+ if (flags[1] == ACTIVE_FLAG && flags[0] != ACTIVE_FLAG)
+ rc = 2;
+ else if (flags[1] == OBSOLETE_FLAG && flags[0] == 0xFF)
+ rc = 2;
+ else
+ rc = 1;
+#else
+ if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
+ rc = 1;
+ else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
+ rc = 2;
+ else if (flags[0] == 0xFF && flags[1] == 0)
+ rc = 2;
+ else if (flags[1] == 0xFF && flags[0] == 0)
+ rc = 1;
+ else /* flags are equal - almost impossible */
+ rc = 1;
+#endif
+ }
+
+ return rc;
+}
+
+
+int env_init(void)
+{
+ env_valid = env_check_valid();
+ if (env_valid == 0) {
+ printf_P(PSTR("*** Warning - bad CRC, "
+ "using default environment\n\n"));
+ set_default_env();
+ }
+ entrycount = 0;
+ envlist_import();
+ return 0;
+}
+
+
+static
+env_item_t *envlist_search(const char *name)
+{
+ return bsearch(name, env_list, entrycount,
+ sizeof(env_item_t), comp_env_key_item);
+}
+
+
+static
+env_item_t *envlist_enter(env_item_t *e)
+{
+ char *key = e->name.ram;
+ const size_t size = sizeof(env_item_t);
+ env_item_t *ep;
+
+ ep = bsearch(key, env_list, entrycount,
+ size, comp_env_key_item);
+
+ if (ep == NULL) {
+ if (entrycount < CONFIG_ENVVAR_MAX) {
+
+ env_list[entrycount++] = *e;
+ qsort(env_list, entrycount, size, comp_env_items);
+ ep = bsearch(key, env_list, entrycount,
+ size, comp_env_key_item);
+ }
+ } else {
+ if ((ep->flags & EF_V_EEP) == 0) {
+ free(ep->val.ram);
+ }
+ ep->val.ram = e->val.ram;
+ }
+
+ if (ep != NULL) {
+ ep->flags |= EF_DIRTY;
+ ep->flags &= ~EF_V_EEP;
+
+ if ((ep->flags & EF_N_EEP) == 0) {
+ int nlen = strnlen(key, CONFIG_SYS_ENV_NAMELEN);
+ char *name = xmalloc(nlen + 1);
+ if (name == NULL) {
+ printf_P(PSTR("## Can't malloc %d bytes\n"),
+ nlen + 1);
+ return NULL;
+ }
+ strcpy(name, key);
+ name[nlen] = '\0';
+ ep->name.ram = name;
+ }
+ }
+
+ return ep;
+}
+
+
+static
+int env_item_delete(env_item_t *ep)
+{
+ if (entrycount == 0)
+ return -1;
+ if ((ep->flags & EF_V_EEP) == 0)
+ free(ep->val.ram);
+ if ((ep->flags & EF_N_EEP) == 0)
+ free(ep->name.ram);
+
+ entrycount--;
+ size_t size = sizeof(env_item_t);
+ memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size);
+
+ return 0;
+}
+
+static
+int envlist_delete(const char *name)
+{
+ env_item_t *ep = bsearch(name, env_list, entrycount,
+ sizeof(env_item_t), comp_env_key_item);
+
+ if (ep != NULL)
+ return env_item_delete(ep);
+
+ return 1;
+}
+
+
+static
+env_item_t *envlist_get(const char *name, uint_fast8_t flag)
+{
+ env_item_t *ep;
+
+ ep = envlist_search(name);
+
+ if (ep != NULL && (flag & ENV_GET_VAL)) {
+ if (ep->flags & EF_V_EEP) {
+ char *vp;
+ uint_fast8_t len;
+ /* TODO: function that gets len of val,
+ to get rid of xrealloc */
+ vp = xmalloc(CONFIG_SYS_CBSIZE);
+ len = varval_get(vp, ep->val.eep, CONFIG_SYS_CBSIZE);
+ ep->val.ram = xrealloc(vp, len + 1);
+ ep->flags &= ~EF_V_EEP;
+ }
+ }
+
+ return ep;
+}
+
+
+char *getenv(const char *name)
+{
+ env_item_t *ep;
+ char *ret = NULL;
+
+ ep = envlist_get(name, ENV_GET_VAL);
+ if (ep != NULL)
+ ret = ep->val.ram;
+ return ret;
+}
+
+static
+int env_item_save(env_item_t *ep, uint16_t offset, int space_left)
+{
+ char buf[CONFIG_SYS_ENV_NAMELEN+1];
+ int len;
+ env_item_t e = *ep;
+
+ len = varname_get(buf, ep);
+ if (len == 0)
+ return 0;
+ buf[len++] = '=';
+ space_left -= len;
+
+ if (space_left <= 0)
+ return 0;
+
+ eeprom_update_block(buf, (uint8_t *) offset, len);
+ offset += len;
+
+ if (e.val.ram != NULL) {
+ char c;
+ do {
+ if (e.flags & EF_V_EEP)
+ c = env_get_char(e.val.eep++);
+ else
+ c = *e.val.ram++;
+
+ eeprom_update_byte((uint8_t *) offset, c);
+ offset++;
+ space_left--;
+ len++;
+ } while ((c != '\0') && space_left );
+ }
+ return len;
+}
+
+
+static
+int saveenv(void)
+{
+ unsigned int off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
+ unsigned int off_red = CONFIG_ENV_OFFSET;
+ unsigned int pos;
+ int len, left;
+ uint16_t crc;
+ int rc = 0;
+
+ if (env_valid == 2) {
+ off = CONFIG_ENV_OFFSET;
+ off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
+ }
+
+ eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff);
+
+ pos = off + offsetof(env_t, data);
+ left = ENV_SIZE - 1;
+ for (int i = 0 ; i < entrycount; i++) {
+ len = env_item_save(&env_list[i], pos, left);
+ if (len == 0) {
+ return 1;
+ }
+ pos += len;
+ left -= len;
+ }
+ /* terminate list */
+ eeprom_update_byte((uint8_t *) pos, 0);
+ crc = env_crc(off + offsetof(env_t, data));
+ eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc);
+ eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags),
+ ACTIVE_FLAG);
+
+ if (rc == 0) {
+ while (entrycount != 0) {
+ env_item_delete(&env_list[entrycount-1]);
+ }
+ eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags),
+ OBSOLETE_FLAG);
+ env_valid = (env_valid == 2) ? 1 : 2;
+
+ envlist_import();
+ }
+
+ return rc;
+}
+
+
+static
+int env_item_print(env_item_t *ep)
+{
+ char buf[CONFIG_SYS_ENV_NAMELEN+1];
+ int len;
+ env_item_t e = *ep;
+
+ varname_get(buf, ep);
+ len = printf_P(PSTR("%s="), buf);
+
+ if (e.val.ram != NULL) {
+ while (1) {
+ char c;
+ if (e.flags & EF_V_EEP)
+ c = env_get_char(e.val.eep++);
+ else
+ c = *e.val.ram++;
+
+ if (c != '\0') {
+ putchar(c);
+ len++;
+ } else
+ break;
+ }
+ }
+ putchar('\n');
+ len ++;
+
+ return len;
+}
+
+
+/*
+ * Command interface: print one or all environment variables
+ *
+ * Returns -1 in case of error, or length of printed string
+ */
+static
+int env_print(char *name)
+{
+ int len = -1;
+
+ if (name) { /* print a single name */
+
+ env_item_t *ep = envlist_search(name);
+ if (ep != NULL)
+ len = env_item_print(ep);
+
+ } else { /* print whole list */
+ len = 0;
+ for (int i = 0 ; i < entrycount; i++) {
+ len += env_item_print(&env_list[i]);
+ }
+ }
+ return len;
+}
+
+static
+int env_print_ramsize(void)
+{
+ int size = 0;
+ uint8_t name_cnt = 0;
+ uint8_t val_cnt = 0;
+
+ for (int i = 0 ; i < entrycount; i++) {
+ if ((env_list[i].flags & EF_N_EEP) == 0 &&
+ (env_list[i].name.ram != NULL)) {
+ name_cnt++;
+ size += strlen(env_list[i].name.ram) + 3;
+ }
+ if ((env_list[i].flags & EF_V_EEP) == 0 &&
+ (env_list[i].val.ram != NULL)) {
+ val_cnt++;
+ size += strlen(env_list[i].val.ram) + 3;
+ }
+ }
+ printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"),
+ size, name_cnt, val_cnt);
+ return size;
+}
+
+
+int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ int i;
+ int rcode = 0;
+
+ (void) cmdtp; (void) flag;
+
+ if (argc == 1) {
+ /* print all env vars */
+ rcode = env_print(NULL);
+ if (rcode < 0)
+ return 1;
+ printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
+ rcode, ENV_SIZE);
+ env_print_ramsize();
+ return 0;
+ }
+
+ /* print selected env vars */
+ for (i = 1; i < argc; ++i) {
+ int rc = env_print(argv[i]);
+ if (rc < 0) {
+ printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
+ ++rcode;
+ }
+ }
+
+ return rcode;
+}
+
+
+/**
+ * Set or delete environment variable
+ *
+ * Set a new environment variable,
+ * or replace or delete an existing one.
+ *
+ * @param flag (not used)
+ * @param argc
+ * @param argv[1] Environment variable to set or delete
+ * if no more args
+ * @param argv[2] ... Value to set it to
+ *
+ * @return 0 if ok, 1 on error
+ */
+static
+int _do_env_set(int flag, int argc, char * const argv[])
+{
+ int i, len;
+ char *name, *value, *s;
+ env_item_t e, *ep;
+
+ (void) flag;
+ debug("Initial value for argc=%d\n", argc);
+
+ name = argv[1];
+ value = argv[2];
+
+ if (strchr(name, '=')) {
+ printf_P(PSTR("## Error: illegal character '='"
+ "in variable name \"%s\"\n"), name);
+ return 1;
+ }
+ if (strlen(name) > CONFIG_SYS_ENV_NAMELEN) {
+ printf_P(PSTR("## Error: Variable name \"%s\" too long. "
+ "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN);
+ return 1;
+ }
+/*
+ env_id++;
+*/
+ /* Delete only ? */
+ if (argc < 3 || argv[2] == NULL) {
+ int rc = envlist_delete(name);
+ return rc != 0;
+ }
+
+ /*
+ * Insert / replace new value
+ */
+ for (i = 2, len = 0; i < argc; ++i)
+ len += strlen(argv[i]) + 1;
+
+ value = xmalloc(len);
+ if (value == NULL) {
+ printf_P(PSTR("## Can't malloc %d bytes\n"), len);
+ return 1;
+ }
+ for (i = 2, s = value; i < argc; ++i) {
+ char *v = argv[i];
+
+ while ((*s++ = *v++) != '\0')
+ ;
+ *(s - 1) = ' ';
+ }
+ if (s != value)
+ *--s = '\0';
+
+ e.flags = EF_DIRTY;
+ e.name.ram = name;
+ e.val.ram = value;
+ ep = envlist_enter(&e);
+ if (!ep) {
+ printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
+ name);
+ return 1;
+ }
+
+ return 0;
+}
+
+/**
+ * Set an environment variable
+ *
+ * @param varname Environment variable to set
+ * @param varvalue Value to set it to
+ * @return 0 if ok, 1 on error
+ */
+static
+int setenv(const char *varname, const char *varvalue)
+{
+ const char * const argv[3] = { NULL, varname, varvalue };
+ int argc = 3;
+
+ if (varvalue == NULL || varvalue[0] == '\0')
+ --argc;
+
+ return _do_env_set(0, argc, (char * const *)argv);
+}
+
+#if 0
+/**
+ * Set an environment variable to an integer value
+ *
+ * @param varname Environment variable to set
+ * @param value Value to set it to
+ * @return 0 if ok, 1 on error
+ */
+int setenv_ulong(const char *varname, unsigned long value)
+{
+ /* TODO: this should be unsigned */
+ char *str = simple_itoa(value);
+
+ return setenv(varname, str);
+}
+#endif
+
+
+/**
+ * Set an environment variable to an value in hex
+ *
+ * @param varname Environment variable to set
+ * @param value Value to set it to
+ * @return 0 if ok, 1 on error
+ */
+int setenv_hex(const char *varname, unsigned long value)
+{
+ char str[sizeof(unsigned long) *2 + 1];
+
+ sprintf_P(str, PSTR("%lx"), value);
+ return setenv(varname, str);
+}
+
+/**
+ * Get an environment variable as a hex value
+ *
+ * @param varname Environment variable to get
+ * @param default_val Return this, if variable does not exist
+ * @return hex value of variable or default_val
+ */
+unsigned long getenv_hex(const char *varname, unsigned long default_val)
+{
+ const char *s;
+ unsigned long value;
+ char *endp;
+
+ s = getenv(varname);
+ if (s)
+ value = strtoul(s, &endp, 16);
+ if (!s || endp == s)
+ return default_val;
+
+ return value;
+}
+
+int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ (void) cmdtp;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+ return _do_env_set(flag, argc, argv);
+}
+
+
+int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ (void) cmdtp; (void) flag; (void) argc; (void) argv;
+
+ printf_P(PSTR("Saving Environment ...\n"));
+ return saveenv() ? 1 : 0;
+}
+
+#if defined(CONFIG_AUTO_COMPLETE)
+int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
+{
+ ENTRY *match;
+ int found, idx;
+
+ idx = 0;
+ found = 0;
+ cmdv[0] = NULL;
+
+ while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
+ int vallen = strlen(match->key) + 1;
+
+ if (found >= maxv - 2 || bufsz < vallen)
+ break;
+
+ cmdv[found++] = buf;
+ memcpy(buf, match->key, vallen);
+ buf += vallen;
+ bufsz -= vallen;
+ }
+
+ qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
+
+ if (idx)
+ cmdv[found++] = "...";
+
+ cmdv[found] = NULL;
+ return found;
+}
+#endif
diff --git a/avr/env.h b/avr/env.h
new file mode 100644
index 0000000..9d55273
--- /dev/null
+++ b/avr/env.h
@@ -0,0 +1,10 @@
+#ifndef ENV_H
+#define ENV_H
+
+int env_init(void);
+
+char *getenv(const char *name);
+int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf);
+
+#endif /* ENV_H */
+
diff --git a/avr/main.c b/avr/main.c
new file mode 100644
index 0000000..5c4cd0c
--- /dev/null
+++ b/avr/main.c
@@ -0,0 +1,212 @@
+/*
+ */
+
+
+#include "common.h"
+
+#include <util/delay.h>
+//#include <avr/power.h>
+//#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+//#include <util/atomic.h>
+//#include <avr/sleep.h>
+//#include <string.h>
+
+#include <util/delay.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+
+
+#include "config.h"
+#include "debug.h"
+#include "z80-if.h"
+#include "con-utils.h"
+#include "serial.h"
+#include "timer.h"
+#include "cli.h"
+#include "env.h"
+
+/*--------------------------------------------------------------------------*/
+
+static uint8_t mcusr;
+
+static
+void setup_avr(void)
+{
+ /* save and clear reset reason(s) */
+ mcusr = MCUSR;
+ MCUSR = 0;
+
+ /* WD */
+
+ /* CPU */
+
+ /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
+ MCUCR = _BV(JTD);
+ MCUCR = _BV(JTD);
+
+ /* disable unused periphels */
+ PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC);
+ PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
+ _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
+
+ /* disable analog comparator */
+ ACSR = _BV(ACD);
+ /* Ports */
+
+ /* Clock */
+ CLKPR = _BV(CLKPCE);
+ CLKPR = 0;
+
+ /* Timer */
+
+ OCR1A = F_CPU / 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
+ TCCR1B = 0b00001010;
+ TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
+}
+
+static const FLASH char * const FLASH rreasons[] = {
+ FSTR("Power on"),
+ FSTR("External"),
+ FSTR("Brown out"),
+ FSTR("Watchdog"),
+ FSTR("JTAG"),
+ };
+
+static
+void print_reset_reason(void)
+{
+ uint8_t r = mcusr & 0x1f;
+ const FLASH char * const FLASH *p = rreasons;
+
+ printf_P(PSTR("Reset reason(s): "));
+ for ( ; r; p++, r >>= 1) {
+ if (r & 1) {
+ my_puts_P(*p);
+ if (r & ~1)
+ printf_P(PSTR(", "));
+ }
+ }
+ printf_P(PSTR(".\n"));
+}
+
+
+/*******************************************************************************/
+/*******************************************************************************/
+
+#define udelay(n) _delay_us(n)
+
+
+/* Stored value of bootdelay, used by autoboot_command() */
+static int stored_bootdelay;
+
+
+/***************************************************************************
+ * Watch for 'delay' seconds for autoboot stop.
+ * returns: 0 - no key, allow autoboot
+ * 1 - got key, abort
+ */
+
+static int abortboot(int bootdelay)
+{
+ int abort = 0;
+ uint32_t ts;
+
+ if (bootdelay >= 0)
+ printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);
+
+#if defined CONFIG_ZERO_BOOTDELAY_CHECK
+ /*
+ * Check if key already pressed
+ * Don't check if bootdelay < 0
+ */
+ if (bootdelay >= 0) {
+ if (tstc()) { /* we got a key press */
+ (void) my_getchar(); /* consume input */
+ my_puts_P(PSTR("\b\b\b 0"));
+ abort = 1; /* don't auto boot */
+ }
+ }
+#endif
+
+ while ((bootdelay > 0) && (!abort)) {
+ --bootdelay;
+ /* delay 1000 ms */
+ ts = get_timer(0);
+ do {
+ if (tstc()) { /* we got a key press */
+ abort = 1; /* don't auto boot */
+ bootdelay = 0; /* no more delay */
+ break;
+ }
+ udelay(10000);
+ } while (!abort && get_timer(ts) < 1000);
+
+ printf_P(PSTR("\b\b\b%2d "), bootdelay);
+ }
+
+ putchar('\n');
+
+ return abort;
+}
+
+static
+const char *bootdelay_process(void)
+{
+ char *s;
+ int bootdelay;
+
+ s = getenv("bootdelay");
+ bootdelay = s ? atoi(s) : CONFIG_BOOTDELAY;
+
+
+ debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
+ _delay_ms(20);
+
+ s = getenv("bootcmd");
+ stored_bootdelay = bootdelay;
+ return s;
+}
+
+static
+void autoboot_command(const char *s)
+{
+ debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>"));
+ _delay_ms(20);
+
+ if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
+ run_command_list(s, -1);
+ }
+}
+
+
+static
+void main_loop(void)
+{
+ const char *s;
+
+ s = bootdelay_process();
+ autoboot_command(s);
+ cli_loop();
+}
+
+int main(void)
+{
+ setup_avr();
+ z80_setup_bus();
+
+ serial_setup();
+ sei();
+
+ debug("\n=========================< (RE)START DEBUG >=========================\n");
+#if DEBUG
+ print_reset_reason();
+#endif
+
+ env_init();
+
+ printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));
+
+ main_loop();
+}
diff --git a/avr/ring.h b/avr/ring.h
new file mode 100644
index 0000000..d57f9aa
--- /dev/null
+++ b/avr/ring.h
@@ -0,0 +1,74 @@
+#ifndef RING_H
+#define RING_H
+
+struct ring {
+ uint8_t *data;
+ uint_fast8_t mask;
+ volatile uint_fast8_t begin;
+ volatile uint_fast8_t end;
+};
+
+
+static inline
+void ring_init(struct ring *ring, uint8_t *buf, int size)
+{
+ ring->data = buf;
+ ring->mask = (size-1) & 0xff;
+ ring->begin = 0;
+ ring->end = 0;
+}
+
+static inline
+int ring_write_ch(struct ring *ring, uint8_t ch)
+{
+ uint_fast8_t ep = ring->end;
+
+ ring->data[ep] = ch;
+ ep = (ep + 1) & ring->mask;
+
+ if ((ep) != ring->begin) {
+ ring->end = ep;
+ return 1;
+ }
+
+ return -1;
+}
+
+#if 0
+static inline
+int ring_write(struct ring *ring, uint8_t *data, int size)
+{
+ int i;
+
+ for (i = 0; i < size; i++) {
+ if (ring_write_ch(ring, data[i]) < 0)
+ return -i;
+ }
+
+ return i;
+}
+#endif
+
+static inline
+int ring_read_ch(struct ring *ring)
+{
+ int ret = -1;
+ uint_fast8_t bp = ring->begin;
+
+ if (bp != ring->end) {
+ ret = ring->data[bp];
+ ring->begin = (bp + 1) & ring->mask;
+ }
+
+ return ret;
+}
+
+
+static inline
+int_fast8_t ring_is_empty(struct ring *ring)
+{
+ return ring->begin == ring->end;
+}
+
+#endif /* RING_H */
+
diff --git a/avr/serial.c b/avr/serial.c
new file mode 100644
index 0000000..b2fea51
--- /dev/null
+++ b/avr/serial.c
@@ -0,0 +1,123 @@
+/*
+ */
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/atomic.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include "ring.h"
+#include "serial.h"
+
+
+static int _write(char c, FILE *stream);
+static FILE mystdout = FDEV_SETUP_STREAM(_write,
+ NULL, _FDEV_SETUP_WRITE);
+
+
+
+#define BUFFER_SIZE 64
+
+#if ((BUFFER_SIZE-1) & BUFFER_SIZE)
+# error: BUFFER_SIZE not power of 2
+#endif
+
+#if ((BUFFER_SIZE) > 256)
+# error: BUFFER_SIZE
+#endif
+
+struct ring rx_ring;
+struct ring tx_ring;
+uint8_t rx_ring_buffer[BUFFER_SIZE];
+uint8_t tx_ring_buffer[BUFFER_SIZE];
+
+
+
+/* Initialize UART */
+
+void usart0_setup(void) {
+
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+
+ PRR0 &= ~_BV(PRUSART0);
+ UCSR0B = 0;
+
+ /* Initialize ring buffers. */
+ ring_init(&rx_ring, rx_ring_buffer, BUFFER_SIZE);
+ ring_init(&tx_ring, tx_ring_buffer, BUFFER_SIZE);
+
+ UCSR0A = 0;
+ UBRR0 = F_CPU / BAUD / 16 - 1;
+ UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
+ UCSR0C = 3 << UCSZ00;
+ };
+}
+
+/*--------------------------------------------------------------------------*/
+
+/* UART RXC interrupt */
+
+ISR(USART0_RX_vect)
+{
+ uint8_t d;
+
+ d = UDR0;
+ ring_write_ch(&rx_ring, d);
+}
+
+/* UART UDRE interrupt */
+
+ISR(USART0_UDRE_vect)
+{
+ int d = ring_read_ch(&tx_ring);
+
+ if (d < 0) {
+ /* Disable TX empty interrupt. */
+ UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
+ } else {
+ UDR0 = d;
+ }
+}
+
+/*--------------------------------------------------------------------------*/
+
+void serial_setup(void)
+{
+ stdout = &mystdout;
+ usart0_setup();
+}
+
+/*--------------------------------------------------------------------------*/
+
+int _write(char c, FILE *stream)
+{
+ (void) stream;
+
+ if (c == '\n')
+ serial_putc('\r');
+ serial_putc(c);
+
+ return 0;
+}
+
+int serial_getc(void)
+{
+ return ring_read_ch(&rx_ring);
+}
+
+void serial_putc(char data)
+{
+ while (ring_write_ch(&tx_ring, data) < 0)
+ ;
+
+ /* Enable the TXE interrupt. */
+ UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0) | _BV(UDRIE0);
+}
+
+uint_fast8_t serial_tstc(void)
+{
+ return !ring_is_empty(&rx_ring);
+}
+
+
diff --git a/avr/serial.h b/avr/serial.h
new file mode 100644
index 0000000..7414ef1
--- /dev/null
+++ b/avr/serial.h
@@ -0,0 +1,9 @@
+#ifndef SERIAL_H
+#define SERIAL_H
+
+void serial_setup(void);
+void serial_putc(char);
+int serial_getc(void);
+uint_fast8_t serial_tstc(void);
+
+#endif /* SERIAL_H */
diff --git a/avr/timer.c b/avr/timer.c
new file mode 100644
index 0000000..84a9737
--- /dev/null
+++ b/avr/timer.c
@@ -0,0 +1,87 @@
+/*
+ */
+
+
+#include "common.h"
+
+//#include <avr/power.h>
+//#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+#include <util/atomic.h>
+
+//#include <stdio.h>
+
+
+#include "timer.h"
+
+/* timer interrupt/overflow counter */
+static volatile uint32_t timestamp;
+
+
+/*---------------------------------------------------------*/
+/* 1000Hz timer interrupt generated by OC1A */
+/*---------------------------------------------------------*/
+
+ISR(TIMER1_COMPA_vect)
+{
+ static int_fast8_t tick_10ms;
+ int_fast8_t i;
+
+
+ timestamp++;
+
+ i = tick_10ms + 1;
+ if (i == 10) {
+ i = 0;
+ Stat |= S_10MS_TO;
+
+ /* Drive timer procedure of low level disk I/O module */
+ //disk_timerproc();
+ }
+ tick_10ms = i;
+
+}
+
+
+
+/*--------------------------------------------------------------------------*/
+
+#if 0
+
+void do_10ms(void)
+{
+ if (to_counter)
+ to_counter--;
+}
+
+#endif
+
+/*--------------------------------------------------------------------------*/
+
+
+void timer_setup(void)
+{
+
+ /* Clock */
+ CLKPR = _BV(CLKPCE);
+ CLKPR = 0;
+
+ /* Timer */
+
+ OCR1A = F_CPU / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
+ TCCR1B = 0b00001001;
+ TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
+}
+
+
+uint32_t get_timer(uint32_t base)
+{
+ uint32_t ret;
+
+ ATOMIC_BLOCK(ATOMIC_FORCEON)
+ {
+ ret = timestamp;
+ }
+ return ret - base;
+}
+
diff --git a/avr/timer.h b/avr/timer.h
new file mode 100644
index 0000000..bed3eb0
--- /dev/null
+++ b/avr/timer.h
@@ -0,0 +1,7 @@
+#ifndef TIMER_H
+#define TIMER_H
+
+uint32_t get_timer(uint32_t);
+
+#endif /* TIMER_H */
+
diff --git a/avr/xmalloc.c b/avr/xmalloc.c
new file mode 100644
index 0000000..9bf2684
--- /dev/null
+++ b/avr/xmalloc.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+
+#include "debug.h"
+#include "xmalloc.h"
+
+void* xmalloc(size_t size)
+{
+ void *p;
+
+ p = malloc(size);
+
+ if (p == NULL)
+ debug("*** Out of memory!\n");
+
+ return p;
+}
+
+
+void* xrealloc(void *p, size_t size)
+{
+ p = realloc(p, size);
+
+ if (p == NULL)
+ debug("*** Out of memory!\n");
+
+ return p;
+}
diff --git a/avr/xmalloc.h b/avr/xmalloc.h
new file mode 100644
index 0000000..cb0019f
--- /dev/null
+++ b/avr/xmalloc.h
@@ -0,0 +1,8 @@
+
+#ifndef XMALLOC_H
+#define XMALLOC_H
+
+void* xmalloc(size_t size);
+void* xrealloc(void *p, size_t size);
+
+#endif /* XMALLOC_H */
diff --git a/avr/z180-serv.c b/avr/z180-serv.c
new file mode 100644
index 0000000..193b23f
--- /dev/null
+++ b/avr/z180-serv.c
@@ -0,0 +1,327 @@
+/*
+ */
+
+#include "common.h"
+//#include <avr/power.h>
+//#include <avr/pgmspace.h>
+//#include <util/atomic.h>
+//#include <avr/sleep.h>
+//#include <string.h>
+
+
+#include "debug.h"
+#include "serial.h"
+#include "z80-if.h"
+
+
+
+/*--------------------------------------------------------------------------*/
+
+uint32_t z80_sram_cmp(uint32_t addr, uint32_t length, uint8_t wval, int inc)
+{
+ uint8_t rval;
+ int_fast8_t errors = 0;
+
+ DBG_P(1, "SRAM: Check 0x%.5lx byte... ", length);
+ while (length--) {
+ if ((rval = z80_read(addr)) != wval) {
+ if (errors == 0) {
+ DBG_P(1, "\nSRAM: Address W R\n" \
+ " ------------------\n");
+ }
+ errors++;
+ if (errors > 20) {
+ DBG_P(1, " ...\n");
+ break;
+ }
+ DBG_P(1, " 0x%.5lx 0x%.2x 0x%.2x\n", addr, wval, rval);
+ }
+ addr++;
+ wval += inc;
+ }
+ DBG_P(1, "Done.\n");
+
+ return addr;
+}
+
+void z80_sram_fill(uint32_t addr, uint32_t length, uint8_t startval, int inc)
+{
+ printf("SRAM: Write 0x%.5lx byte... ", length);
+ while (length--) {
+ z80_write(addr, startval);
+ ++addr;
+ startval += inc;
+ }
+ printf("Done.\n");
+}
+
+
+#if 0
+void z80_sram_fill_string(uint32_t addr, int length, const char *text)
+{
+ char c;
+ const char *p = text;
+
+ while (length--) {
+ z80_write(addr++, c = *p++);
+ if (c == 0)
+ p = text;
+ }
+}
+
+
+uint32_t z80_sram_cmp_string(uint32_t addr, int length, const char *text)
+{
+ char c;
+ const char *p = text;
+
+ while (length--) {
+ c = *p++;
+ if (z80_read(addr) != c)
+ break;
+ ++addr;
+ if (c == 0)
+ p = text;
+ }
+ return addr;
+}
+
+const char * const qbfox = "Zhe quick brown fox jumps over the lazy dog!";
+const char * const qbcat = "Zhe quick brown fox jumps over the lazy cat!";
+
+#endif
+
+uint8_t z80_get_byte(uint32_t adr)
+{
+ uint8_t data;
+
+ z80_bus_cmd(Request);
+ data = z80_read(adr),
+ z80_bus_cmd(Release);
+
+ return data;
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+struct msg_item {
+ uint8_t fct;
+ uint8_t sub_min, sub_max;
+ void (*func)(uint8_t, int, uint8_t *);
+};
+
+uint32_t msg_to_addr(uint8_t *msg)
+{
+ union {
+ uint32_t as32;
+ uint8_t as8[4];
+ } addr;
+
+ addr.as8[0] = msg[0];
+ addr.as8[1] = msg[1];
+ addr.as8[2] = msg[2];
+ addr.as8[3] = 0;
+
+ return addr.as32;
+}
+
+void do_msg_ini_msgfifo(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)subf; (void)len;
+
+ z80_init_msg_fifo(msg_to_addr(msg));
+}
+
+
+void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)len;
+
+ z80_memfifo_init(subf - 1, msg_to_addr(msg));
+}
+
+
+void do_msg_char_out(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)subf;
+
+ while (len--)
+ putchar(*msg++);
+}
+
+
+const FLASH struct msg_item z80_messages[] =
+{
+ { 0, /* fct nr. */
+ 0, 0, /* sub fct nr. from, to */
+ do_msg_ini_msgfifo},
+ { 0,
+ 1, 2,
+ do_msg_ini_memfifo},
+ { 1,
+ 1, 1,
+ do_msg_char_out},
+ { 0xff, /* end mark */
+ 0, 0,
+ 0},
+
+};
+
+
+
+
+void do_message(int len, uint8_t *msg)
+{
+ uint8_t fct, sub_fct;
+ int_fast8_t i = 0;
+
+ if (len >= 2) {
+ fct = *msg++;
+ sub_fct = *msg++;
+ len -= 2;
+
+ while (fct != z80_messages[i].fct)
+ ++i;
+
+ if (z80_messages[i].fct == 0xff) {
+ DBG_P(1, "do_message: Unknown function: %i, %i\n",
+ fct, sub_fct);
+ return; /* TODO: unknown message # */
+ }
+
+ while (fct == z80_messages[i].fct) {
+ if (sub_fct >= z80_messages[i].sub_min && sub_fct <= z80_messages[i].sub_max )
+ break;
+ ++i;
+ }
+
+ if (z80_messages[i].fct != fct) {
+ DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
+ fct, sub_fct);
+ return; /* TODO: unknown message sub# */
+ }
+
+ (z80_messages[i].func)(sub_fct, len, msg);
+
+
+ } else {
+ /* TODO: error */
+ DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len);
+ }
+}
+
+
+
+#define CTRBUF_LEN 256
+
+void check_msg_fifo(void)
+{
+ int ch;
+ static int_fast8_t state;
+ static int msglen,idx;
+ static uint8_t buffer[CTRBUF_LEN];
+
+ while (state != 3 && (ch = z80_msg_fifo_getc()) >= 0) {
+ switch (state) {
+ case 0: /* wait for start of message */
+ if (ch == 0x81) {
+ msglen = 0;
+ idx = 0;
+ state = 1;
+ }
+ break;
+ case 1: /* get msg len */
+ if (ch > 0 && ch <= CTRBUF_LEN) {
+ msglen = ch;
+ state = 2;
+ } else
+ state = 0;
+ break;
+ case 2: /* get message */
+ buffer[idx++] = ch;
+ if (idx == msglen)
+ state = 3;
+ break;
+ }
+ }
+
+ if (state == 3) {
+ do_message(msglen, buffer);
+ state = 0;
+ }
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+#if 0
+void dump_mem(const FLASH uint8_t *addr, uint32_t len)
+{
+ DBG_P(1, "hdrom dump:");
+ while (len) {
+ DBG_P(1, "\n %.5x:", addr);
+ for (unsigned i = 0; i<16; i++)
+ DBG_P(1, " %.2x", *addr++);
+ len -= len > 16 ? 16 : len;
+ }
+ DBG_P(1, "\n");
+}
+#endif
+/*--------------------------------------------------------------------------*/
+
+
+const FLASH uint8_t iniprog[] = {
+ 0xAF, // xor a
+ 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
+ 0x3E, 0x30, // ld a,030h
+ 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states
+};
+
+const FLASH uint8_t sertest[] = {
+ 0xAF, // xor a
+ 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
+ 0x3E, 0x30, // ld a,030h
+ 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
+ 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0
+ 0xED, 0x39, 0x03, // out0 (cntlb1),a
+ 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ;
+ 0xED, 0x39, 0x01, // out0 (cntla1),a
+ 0x3E, 0x00, // ld a,0
+ 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts
+ 0xED, 0x38, 0x05, //l0:in0 a,(stat1)
+ 0xE6, 0x80, // and 80h
+ 0x28, 0xF9, // jr z,l0
+ 0xED, 0x00, 0x09, // in0 b,(rdr1)
+ 0xED, 0x38, 0x05, //l1:in0 a,(stat1)
+ 0xE6, 0x02, // and 02h
+ 0x28, 0xF9, // jr z,l1
+ 0xED, 0x01, 0x07, // out0 (tdr1),b
+ 0x18, 0xEA, // jr l0
+};
+
+const FLASH uint8_t test1[] = {
+ 0xAF, // xor a
+ 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
+ 0x3E, 0x30, // ld a,030h
+ 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
+ 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers
+ 0x06, 0x08, // ld b,dmct_e-dmclrt
+ 0x0E, 0x20, // ld c,sar0l
+ 0xED, 0x93, // otimr
+ 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
+ 0xED, 0x39, 0x31, // out0 (dmode),a ;
+ 0x3E, 0x62, // ld a,062h ;enable dma0,
+ 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
+ 0x18, 0xFB, // jr cl_1 ;
+ 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
+ 0x00, // db 0 ;src
+ 0x00, 0x00, // dw 0 ;dst (inc),
+ 0x00, // db 0 ;dst
+ 0x00, 0x00, // dw 0 ;count (64k)
+};
+
+
+
+// check_msg_fifo();
+
diff --git a/avr/z180-stamp-avr.c b/avr/z180-stamp-avr.c
new file mode 100644
index 0000000..e6edb33
--- /dev/null
+++ b/avr/z180-stamp-avr.c
@@ -0,0 +1,591 @@
+/*
+ */
+
+
+#include <avr/io.h>
+//#include <avr/power.h>
+//#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+//#include <util/atomic.h>
+//#include <avr/sleep.h>
+//#include <string.h>
+
+#include <stdio.h>
+
+
+#include "debug.h"
+#include "serial.h"
+#include "z80-if.h"
+
+#define const const __flash
+#include "../z180/hdrom.h"
+#undef const
+
+#define FLASH __flash
+//#define FLASH
+
+#define ESCCHAR ('^'-0x40)
+
+#define S_10MS_TO (1<<0)
+
+
+volatile int_fast8_t timeout_1s;
+//volatile uint_least8_t Stat;
+#define Stat GPIOR0
+
+unsigned int to_counter;
+
+/****************************************************************/
+
+#define P_ADL PORTA
+#define P_ADH PORTC
+#define P_ADB PORTE
+#define PIN_ADB PINE
+
+#define ADB_WIDTH 3
+#define ADB_SHIFT 2
+//#define ADB_PORT GPIOE
+
+#define MASK(n) ((1<<(n))-1)
+#define SMASK(w,s) (MASK(w) << (s))
+
+typedef union {
+ uint32_t l;
+ uint16_t w[2];
+ uint8_t b[4];
+} addr_t;
+
+
+
+/*--------------------------------------------------------------------------*/
+
+/*---------------------------------------------------------*/
+/* 1000Hz timer interrupt generated by OC1A */
+/*---------------------------------------------------------*/
+
+ISR(TIMER1_COMPA_vect)
+{
+ static int_fast8_t tick_10ms;
+// static int_fast16_t count_ms;
+
+ int_fast8_t i;
+
+
+ i = tick_10ms + 1;
+ if (i == 10) {
+ i = 0;
+ Stat |= S_10MS_TO;
+
+ /* Drive timer procedure of low level disk I/O module */
+ //disk_timerproc();
+ }
+ tick_10ms = i;
+
+#if 0
+ count_ms++;
+ if (count_ms == 1000) {
+ count_ms = 0;
+
+ i = timeout_1s;
+ if (i)
+ timeout_1s = i - 1;
+ }
+#endif
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+static uint32_t z80_sram_cmp(uint32_t addr, uint32_t length, uint8_t wval, int inc)
+{
+ uint8_t rval;
+ int_fast8_t errors = 0;
+
+ DBG_P(1, "SRAM: Check 0x%.5lx byte... ", length);
+ while (length--) {
+ if ((rval = z80_read(addr)) != wval) {
+ if (errors == 0) {
+ DBG_P(1, "\nSRAM: Address W R\n" \
+ " ------------------\n");
+ }
+ errors++;
+ if (errors > 20) {
+ DBG_P(1, " ...\n");
+ break;
+ }
+ DBG_P(1, " 0x%.5lx 0x%.2x 0x%.2x\n", addr, wval, rval);
+ }
+ addr++;
+ wval += inc;
+ }
+ DBG_P(1, "Done.\n");
+
+ return addr;
+}
+
+static void z80_sram_fill(uint32_t addr, uint32_t length, uint8_t startval, int inc)
+{
+ printf("SRAM: Write 0x%.5lx byte... ", length);
+ while (length--) {
+ z80_write(addr, startval);
+ ++addr;
+ startval += inc;
+ }
+ printf("Done.\n");
+}
+
+
+#if 0
+void z80_sram_fill_string(uint32_t addr, int length, const char *text)
+{
+ char c;
+ const char *p = text;
+
+ while (length--) {
+ z80_write(addr++, c = *p++);
+ if (c == 0)
+ p = text;
+ }
+}
+
+
+uint32_t z80_sram_cmp_string(uint32_t addr, int length, const char *text)
+{
+ char c;
+ const char *p = text;
+
+ while (length--) {
+ c = *p++;
+ if (z80_read(addr) != c)
+ break;
+ ++addr;
+ if (c == 0)
+ p = text;
+ }
+ return addr;
+}
+
+const char * const qbfox = "Zhe quick brown fox jumps over the lazy dog!";
+const char * const qbcat = "Zhe quick brown fox jumps over the lazy cat!";
+
+#endif
+
+uint8_t z80_get_byte(uint32_t adr)
+{
+ uint8_t data;
+
+ z80_request_bus();
+ data = z80_read(adr),
+ z80_release_bus();
+
+ return data;
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+static void do_10ms(void)
+{
+ if (to_counter)
+ to_counter--;
+}
+
+/*--------------------------------------------------------------------------*/
+
+struct msg_item {
+ uint8_t fct;
+ uint8_t sub_min, sub_max;
+ void (*func)(uint8_t, int, uint8_t *);
+};
+
+uint32_t msg_to_addr(uint8_t *msg)
+{
+ union {
+ uint32_t as32;
+ uint8_t as8[4];
+ } addr;
+
+ addr.as8[0] = msg[0];
+ addr.as8[1] = msg[1];
+ addr.as8[2] = msg[2];
+ addr.as8[3] = 0;
+
+ return addr.as32;
+}
+
+void do_msg_ini_msgfifo(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)subf; (void)len;
+
+ z80_init_msg_fifo(msg_to_addr(msg));
+}
+
+
+void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)len;
+
+ z80_memfifo_init(subf - 1, msg_to_addr(msg));
+}
+
+
+void do_msg_char_out(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)subf;
+
+ while (len--)
+ putchar(*msg++);
+}
+
+
+const FLASH struct msg_item z80_messages[] =
+{
+ { 0, /* fct nr. */
+ 0, 0, /* sub fct nr. from, to */
+ do_msg_ini_msgfifo},
+ { 0,
+ 1, 2,
+ do_msg_ini_memfifo},
+ { 1,
+ 1, 1,
+ do_msg_char_out},
+ { 0xff, /* end mark */
+ 0, 0,
+ 0},
+
+};
+
+
+
+
+void do_message(int len, uint8_t *msg)
+{
+ uint8_t fct, sub_fct;
+ int_fast8_t i = 0;
+
+ if (len >= 2) {
+ fct = *msg++;
+ sub_fct = *msg++;
+ len -= 2;
+
+ while (fct != z80_messages[i].fct)
+ ++i;
+
+ if (z80_messages[i].fct == 0xff) {
+ DBG_P(1, "do_message: Unknown function: %i, %i\n",
+ fct, sub_fct);
+ return; /* TODO: unknown message # */
+ }
+
+ while (fct == z80_messages[i].fct) {
+ if (sub_fct >= z80_messages[i].sub_min && sub_fct <= z80_messages[i].sub_max )
+ break;
+ ++i;
+ }
+
+ if (z80_messages[i].fct != fct) {
+ DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
+ fct, sub_fct);
+ return; /* TODO: unknown message sub# */
+ }
+
+ (z80_messages[i].func)(sub_fct, len, msg);
+
+
+ } else {
+ /* TODO: error */
+ DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len);
+ }
+}
+
+
+
+#define CTRBUF_LEN 256
+
+void check_msg_fifo(void)
+{
+ int ch;
+ static int_fast8_t state;
+ static int msglen,idx;
+ static uint8_t buffer[CTRBUF_LEN];
+
+ while (state != 3 && (ch = z80_msg_fifo_getc()) >= 0) {
+ switch (state) {
+ case 0: /* wait for start of message */
+ if (ch == 0x81) {
+ msglen = 0;
+ idx = 0;
+ state = 1;
+ }
+ break;
+ case 1: /* get msg len */
+ if (ch > 0 && ch <= CTRBUF_LEN) {
+ msglen = ch;
+ state = 2;
+ } else
+ state = 0;
+ break;
+ case 2: /* get message */
+ buffer[idx++] = ch;
+ if (idx == msglen)
+ state = 3;
+ break;
+ }
+ }
+
+ if (state == 3) {
+ do_message(msglen, buffer);
+ state = 0;
+ }
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+void dump_mem(const __flash uint8_t *addr, uint32_t len)
+{
+ DBG_P(1, "hdrom dump:");
+ while (len) {
+ DBG_P(1, "\n %.5x:", addr);
+ for (unsigned i = 0; i<16; i++)
+ DBG_P(1, " %.2x", *addr++);
+ len -= len > 16 ? 16 : len;
+ }
+ DBG_P(1, "\n");
+}
+
+/*--------------------------------------------------------------------------*/
+
+void z80_load_mem(void)
+{
+ unsigned sec = 0;
+ uint32_t sec_base = hdrom_start;
+
+ DBG_P(1, "Loading z80 memory... \n");
+
+ while (sec < hdrom_sections) {
+ DBG_P(2, " From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n",
+ hdrom_address[sec],
+ hdrom_address[sec]+hdrom_length_of_sections[sec] - 1,
+ hdrom_length_of_sections[sec]);
+
+ z80_write_block((const __flash unsigned char *) &hdrom[sec_base], /* src */
+ hdrom_address[sec], /* dest */
+ hdrom_length_of_sections[sec]); /* len */
+ sec_base+=hdrom_length_of_sections[sec];
+ sec++;
+ }
+}
+
+/*--------------------------------------------------------------------------*/
+
+void z80_dump_mem(uint32_t addr, uint32_t len)
+{
+ DBG_P(1, "Memory dump:");
+ while (len) {
+ DBG_P(1, "\n %.5lx:", addr);
+ for (unsigned i = 0; i<16; i++)
+ DBG_P(1, " %.2x", z80_read(addr++));
+ len -= len > 16 ? 16 : len;
+ }
+ DBG_P(1, "\n");
+}
+
+/*--------------------------------------------------------------------------*/
+
+void setup_rtc(void)
+{
+ /* TODO: */
+}
+
+void setup_avr(void)
+{
+ /* WD */
+
+ /* CPU */
+
+ /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
+ MCUCR = _BV(JTD);
+ MCUCR = _BV(JTD);
+
+ /* disable unused periphels */
+ PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC);
+ PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
+ _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
+
+ /* disable analog comparator */
+ ACSR = _BV(ACD);
+ /* Ports */
+
+ /* Clock */
+ CLKPR = _BV(CLKPCE);
+ CLKPR = 0;
+
+ /* Timer */
+
+ OCR1A = F_CPU / 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
+ TCCR1B = 0b00001010;
+ TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
+}
+
+const __flash uint8_t iniprog[] = {
+ 0xAF, // xor a
+ 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
+ 0x3E, 0x30, // ld a,030h
+ 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states
+};
+
+const __flash uint8_t sertest[] = {
+ 0xAF, // xor a
+ 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
+ 0x3E, 0x30, // ld a,030h
+ 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
+ 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0
+ 0xED, 0x39, 0x03, // out0 (cntlb1),a
+ 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ;
+ 0xED, 0x39, 0x01, // out0 (cntla1),a
+ 0x3E, 0x00, // ld a,0
+ 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts
+ 0xED, 0x38, 0x05, //l0:in0 a,(stat1)
+ 0xE6, 0x80, // and 80h
+ 0x28, 0xF9, // jr z,l0
+ 0xED, 0x00, 0x09, // in0 b,(rdr1)
+ 0xED, 0x38, 0x05, //l1:in0 a,(stat1)
+ 0xE6, 0x02, // and 02h
+ 0x28, 0xF9, // jr z,l1
+ 0xED, 0x01, 0x07, // out0 (tdr1),b
+ 0x18, 0xEA, // jr l0
+};
+
+const __flash uint8_t test1[] = {
+ 0xAF, // xor a
+ 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
+ 0x3E, 0x30, // ld a,030h
+ 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
+ 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers
+ 0x06, 0x08, // ld b,dmct_e-dmclrt
+ 0x0E, 0x20, // ld c,sar0l
+ 0xED, 0x93, // otimr
+ 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
+ 0xED, 0x39, 0x31, // out0 (dmode),a ;
+ 0x3E, 0x62, // ld a,062h ;enable dma0,
+ 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
+ 0x18, 0xFB, // jr cl_1 ;
+ 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
+ 0x00, // db 0 ;src
+ 0x00, 0x00, // dw 0 ;dst (inc),
+ 0x00, // db 0 ;dst
+ 0x00, 0x00, // dw 0 ;count (64k)
+};
+
+
+int main(void)
+{
+ int_fast8_t state = 0;
+ int ch;
+
+ setup_avr();
+ serial_setup();
+ setup_rtc();
+ sei();
+
+ printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));
+
+ DBG_P(1, "z80_setup_bus... ");
+ z80_setup_msg_fifo();
+ z80_setup_bus();
+ DBG_P(1, "done.\n");
+
+ DBG_P(1, "Get bus... ");
+/* Now done via S_Z180_RUNNING
+ z80_busreq(LOW);
+ z80_reset(HIGH);
+*/
+ z80_request_bus();
+ DBG_P(1, "got it!\n");
+
+// z80_sram_fill(0, (uint32_t)512 * 1024, 0x00, 3);
+// z80_sram_cmp(0, (uint32_t)512 * 1024, 0x00, 3);
+// z80_dump_mem(0, 0x400);
+
+ z80_memset(0, 0x76, 0x80000);
+// z80_memset(0, 0x00, 0x80000);
+// z80_write_block(test1, 0, sizeof(test1));
+
+// z80_dump_mem(0, 0x100);
+
+// z80_sram_cmp(0, (uint32_t)512 * 1024, 0x76, 0);
+
+ z80_load_mem();
+// z80_write(0, 0x76);
+// z80_dump_mem(0, 0x200);
+
+
+/* Now done via S_Z180_RUNNING
+ z80_reset(LOW);
+*/
+ z80_release_bus();
+ DBG_P(1, "Bus released!\n");
+ z80_reset(HIGH);
+ DBG_P(1, "Reset released!\n");
+
+ to_counter = 200;
+
+ while (1) {
+
+ if (Stat & S_10MS_TO) {
+ Stat &= ~S_10MS_TO;
+ do_10ms();
+ }
+
+
+ if ((ch = serial_getc()) >= 0) {
+ switch (state) {
+ case 0:
+ if (ch == ESCCHAR) {
+ state = 1;
+ /* TODO: Timer starten */
+ } else {
+// z80_memfifo_putc(fifo_out, ch);
+ serial_putc(ch);
+ if (ch == '\r')
+ serial_putc('\n');
+ }
+ break;
+ case 1:
+ switch (ch) {
+
+ case 'r':
+ z80_reset_pulse();
+ break;
+
+ case 'b':
+ z80_request_bus();
+ z80_dump_mem(0, 0x2d20);
+ z80_release_bus();
+ break;
+
+ case 'e':
+ z80_request_bus();
+ z80_dump_mem(0x80000-0x4000, 0x800);
+ z80_dump_mem(0x80000-0x200, 0x200);
+ z80_release_bus();
+ break;
+
+ case ESCCHAR:
+ default:
+// z80_memfifo_putc(fifo_out, ch);
+ serial_putc(ch);
+ if (ch == '\r')
+ serial_putc('\n');
+ }
+ state = 0;
+ break;
+ }
+ }
+
+// check_msg_fifo();
+ }
+
+ return 0;
+}
diff --git a/avr/z80-if.c b/avr/z80-if.c
new file mode 100644
index 0000000..392597e
--- /dev/null
+++ b/avr/z80-if.c
@@ -0,0 +1,672 @@
+/**
+ *
+ * Pin assignments
+ *
+ * | Z180-Sig | AVR-Port | Dir | Special Function |
+ * +------------+---------------+-------+-----------------------+
+ * | A0 | PA 0 | O | |
+ * | A1 | PA 1 | O | |
+ * | A2 | PA 2 | O | |
+ * | A3 | PA 3 | O | |
+ * | A4 | PA 4 | O | |
+ * | A5 | PA 5 | O | |
+ * | A6 | PA 6 | O | |
+ * | A7 | PA 7 | O | |
+ * | A8 | PC 0 | O | |
+ * | A9 | PC 1 | O | |
+ * | A10 | PC 2 | O | |
+ * | A11 | PC 3 | O | |
+ * | A12 | PC 4 | O | |
+ * | A13 | PC 5 | O | |
+ * | A14 | PC 6 | O | |
+ * | A15 | PC 7 | O | |
+ * | A16 | PE 2 | O | |
+ * | A17 | PE 3 | O | |
+ * | A18 | PE 4 | O | |
+ * | D0 | PF 0 | I/O | |
+ * | D1 | PF 1 | I/O | |
+ * | D2 | PF 2 | I/O | |
+ * | D3 | PF 3 | I/O | |
+ * | D4 | PF 4 | I/O | |
+ * | D5 | PF 5 | I/O | |
+ * | D6 | PF 6 | I/O | |
+ * | D7 | PF 7 | I/O | |
+ * | RD | PD 3 | O | |
+ * | WR | PD 2 | O | |
+ * | MREQ | PD 4 | O | |
+ * | RST | PD 5 | O | |
+ * | BUSREQ | PD 7 | O | |
+ * | BUSACK | PD 6 | I | |
+ * | IOCS1 | PE 5 | I | |
+ * |* HALT | P | | |
+ * |* NMI | P | | |
+ * | | P | | |
+ * | | P | | af1 USART1_TX |
+ * | | P | | af1 USART1_RX |
+ * | | P |JTDI | remap SPI1_NSS' |
+ * | | P |JTDO | remap SPI1_SCK' |
+ * | | P |JTRST | remap SPI1_MISO' |
+ * | | P | | remap SPI1_MOSI' |
+ * | | P | | af1 OSC32 |
+ * | | P | | af1 OSC32 |
+
+
+ */
+
+#include <avr/io.h>
+#include <util/delay.h>
+#include <util/atomic.h>
+#include <stdio.h>
+#include "debug.h"
+#include "z80-if.h"
+
+
+/* Number of array elements */
+#define NELEMS(x) (sizeof x/sizeof *x)
+
+
+#define CONCAT(x,y) x ## y
+#define EVALUATOR(x,y) CONCAT(x,y)
+
+#define GPIO_(X) CONCAT(GPIO, X)
+
+struct bits {
+ uint8_t b0:1;
+ uint8_t b1:1;
+ uint8_t b2:1;
+ uint8_t b3:1;
+ uint8_t b4:1;
+ uint8_t b5:1;
+ uint8_t b6:1;
+ uint8_t b7:1;
+} __attribute__((__packed__));
+
+#define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)
+
+
+
+#define P_MREQ PORTD
+#define MREQ 4
+#define DDR_MREQ DDRD
+#define P_RD PORTD
+#define RD 3
+#define P_WR PORTD
+#define WR 2
+#define P_BUSREQ PORTD
+#define BUSREQ 7
+#define DDR_BUSREQ DDRD
+#define P_BUSACK PORTD
+#define PIN_BUSACK PIND
+#define BUSACK 6
+#define DDR_BUSACK DDRD
+//#define P_HALT PORTA
+//#define HALT 12
+#define P_IOCS1 PORTE
+#define IOCS1 5
+#define DDR_IOCS1 DDRE
+//#define P_NMI PORTB
+//#define NMI 7
+#define P_RST PORTD
+#define DDR_RST DDRD
+#define RST 5
+
+
+#define P_DB PORTF
+#define PIN_DB PINF
+#define DDR_DB DDRF
+
+#define P_ADL PORTA
+#define P_ADH PORTC
+#define P_ADB PORTE
+#define PIN_ADB PINE
+#define DDR_ADL DDRA
+#define DDR_ADH DDRC
+#define DDR_ADB DDRE
+
+#define ADB_WIDTH 3
+#define ADB_SHIFT 2
+//#define ADB_PORT PORTE
+
+
+#define Z80_O_MREQ SBIT(P_MREQ, 4)
+#define Z80_O_RD SBIT(P_RD, 3)
+#define Z80_O_WR SBIT(P_WR, 2)
+#define Z80_O_BUSREQ SBIT(P_BUSREQ, 7)
+//#define Z80_O_NMI SBIT(P_NMI, )
+#define Z80_O_RST SBIT(P_RST, 5)
+#define Z80_I_BUSACK SBIT(PIN_BUSACK, 6)
+//#define Z80_I_HALT SBIT(P_HALT, )
+
+
+#define MASK(n) ((1<<(n))-1)
+#define SMASK(w,s) (MASK(w) << (s))
+
+
+
+typedef union {
+ uint32_t l;
+ uint16_t w[2];
+ uint8_t b[4];
+} addr_t;
+
+
+static zstate_t zstate;
+
+/*--------------------------------------------------------------------------*/
+
+
+static void z80_setup_addrbus_tristate(void)
+{
+ /* /MREQ, /RD, /WR: Input, no pullup */
+ DDR_MREQ &= ~(_BV(MREQ) | _BV(RD) | _BV(WR));
+ Z80_O_MREQ = 0;
+ Z80_O_RD = 0;
+ Z80_O_WR = 0;
+
+ P_ADL = 0;
+ DDR_ADL = 0;
+ P_ADH = 0;
+ DDR_ADH = 0;
+ PIN_ADB = P_ADB & ~(MASK(ADB_WIDTH) << ADB_SHIFT);
+ DDR_ADB = DDR_ADB & ~(MASK(ADB_WIDTH) << ADB_SHIFT);
+}
+
+
+static void z80_setup_addrbus_active(void)
+{
+ /* /MREQ, /RD, /WR: Output and high */
+ Z80_O_MREQ = 1;
+ Z80_O_RD = 1;
+ Z80_O_WR = 1;
+ DDR_MREQ |= _BV(MREQ) | _BV(RD) | _BV(WR);
+
+ DDR_ADL = 0xff;
+ DDR_ADH = 0xff;
+ DDR_ADB = DDR_ADB | (MASK(ADB_WIDTH) << ADB_SHIFT);
+}
+
+
+static void z80_setup_dbus_in(void)
+{
+ DDR_DB = 0;
+ P_DB = 0;
+}
+
+
+static void z80_setup_dbus_out(void)
+{
+ DDR_DB = 0xff;
+}
+
+
+static void z80_reset_pulse(void)
+{
+ Z80_O_RST = 0;
+ _delay_us(10);
+ Z80_O_RST = 1;
+}
+
+
+void z80_setup_bus(void)
+{
+ /* /ZRESET: Output and low */
+ Z80_O_RST = 0;
+ DDR_RST |= _BV(RST);
+
+ /* /BUSREQ: Output and high */
+ Z80_O_BUSREQ = 1;
+ DDR_BUSREQ |= _BV(BUSREQ);
+
+ /* /BUSACK: Input, no pullup */
+ DDR_BUSACK &= ~_BV(BUSACK);
+ P_BUSACK &= ~_BV(BUSACK);
+
+ /* /IOCS1: Input, no pullup */
+ DDR_IOCS1 &= ~_BV(IOCS1);
+ P_IOCS1 &= ~_BV(IOCS1);
+
+ z80_setup_addrbus_tristate();
+ z80_setup_dbus_in();
+
+ zstate = RESET;
+}
+
+
+zstate_t z80_bus_state(void)
+{
+ return zstate;
+}
+
+
+static void z80_busreq_hpulse(void)
+{
+ z80_setup_dbus_in();
+ z80_setup_addrbus_tristate();
+
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ Z80_O_BUSREQ = 1;
+ Z80_O_BUSREQ = 1; /* 2 AVR clock cycles */
+ Z80_O_BUSREQ = 0; /* 2 AVR clock cycles */
+ }
+
+ if (zstate & ZST_ACQUIRED) {
+ while(Z80_I_BUSACK == 1)
+ ;
+ z80_setup_addrbus_active();
+ }
+}
+
+
+/*
+
+ + | | | | |
+ + State | RESET | RESET_AQRD | RUNNING | RUNNING_AQRD |
+ + | | | | |
+ + | 0 | 1 | 2 | 3 |
+Event + | | | | |
+----------------+---------------+---------------+---------------+---------------+
+ | | | | |
+Reset | 0 | 0 | 0 | 0 |
+ | | | | |
+ | | | | |
+Request | 1 | | 3 | |
+ | | | | |
+ | | | | |
+Release | | 0 | | 2 |
+ | | | | |
+ | | | | |
+Run | 2 | 3 | | |
+ | | | | |
+ | | | | |
+Restart | | | 2 | 3 |
+ | | | | |
+ | | | | |
+M_Cycle | | | | 3 |
+ | | | | |
+ | | | | |
+*/
+
+zstate_t z80_bus_cmd(bus_cmd_t cmd)
+{
+ switch (cmd) {
+
+ case Reset:
+ z80_setup_dbus_in();
+ z80_setup_addrbus_tristate();
+ Z80_O_RST = 0;
+ Z80_O_BUSREQ = 1;
+ zstate = RESET;
+ break;
+
+ case Request:
+ switch (zstate) {
+ case RESET:
+ Z80_O_BUSREQ = 0;
+ Z80_O_RST = 1;
+ while(Z80_I_BUSACK == 1)
+ ;
+ z80_setup_addrbus_active();
+ zstate = RESET_AQRD;
+ break;
+
+ case RUNNING:
+ Z80_O_BUSREQ = 0;
+ while(Z80_I_BUSACK == 1)
+ ;
+ z80_setup_addrbus_active();
+ zstate = RUNNING_AQRD;
+ break;
+
+ default:
+ break;
+ }
+ break;
+
+ case Release:
+ switch (zstate) {
+ case RESET_AQRD:
+ z80_setup_dbus_in();
+ z80_setup_addrbus_tristate();
+ Z80_O_RST = 0;
+ Z80_O_BUSREQ = 1;
+ zstate = RESET;
+ break;
+ case RUNNING_AQRD:
+ z80_setup_dbus_in();
+ z80_setup_addrbus_tristate();
+ Z80_O_BUSREQ = 1;
+ zstate = RUNNING;
+ break;
+ default:
+ break;
+ }
+ break;
+
+ case Run:
+ switch (zstate) {
+ case RESET:
+ Z80_O_RST = 1;
+ zstate = RUNNING;
+ break;
+
+ case RESET_AQRD:
+ z80_setup_dbus_in();
+ z80_setup_addrbus_tristate();
+ z80_reset_pulse();
+ z80_setup_addrbus_active();
+ zstate = RUNNING_AQRD;
+ break;
+ default:
+ break;
+ }
+ break;
+
+ case Restart:
+ switch (zstate) {
+ case RUNNING:
+ case RUNNING_AQRD:
+ z80_reset_pulse();
+ break;
+ default:
+ break;
+ }
+ break;
+
+ case M_Cycle:
+ switch (zstate) {
+ case RUNNING_AQRD:
+ z80_busreq_hpulse();
+ break;
+ default:
+ break;
+ }
+ }
+ return zstate;
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+static
+//inline __attribute__ ((always_inline))
+void z80_setaddress(uint32_t addr)
+{
+ addr_t x; x.l = addr;
+
+ P_ADL = x.b[0];
+ P_ADH = x.b[1];
+ PIN_ADB = ((x.b[2] << ADB_SHIFT) ^ P_ADB) & MASK(ADB_WIDTH) << ADB_SHIFT ;
+}
+
+void z80_write(uint32_t addr, uint8_t data)
+{
+ z80_setaddress(addr);
+ Z80_O_MREQ = 0;
+ z80_setup_dbus_out();
+ P_DB = data;
+ P_DB = data;
+ Z80_O_WR = 0;
+ Z80_O_WR = 0;
+ Z80_O_WR = 1;
+ Z80_O_MREQ = 1;
+}
+
+uint8_t z80_read(uint32_t addr)
+{
+ uint8_t data;
+
+ z80_setaddress(addr);
+ Z80_O_MREQ = 0;
+ z80_setup_dbus_in();
+ Z80_O_RD = 0;
+ Z80_O_RD = 0;
+ Z80_O_RD = 0;
+ data = PIN_DB;
+ Z80_O_RD = 1;
+ Z80_O_MREQ = 1;
+
+ return data;
+}
+
+
+void z80_memset(uint32_t addr, uint8_t data, uint32_t length)
+{
+ z80_setup_dbus_out();
+ Z80_O_MREQ = 0;
+ while(length--) {
+ z80_setaddress(addr++);
+ P_DB = data;
+ P_DB = data;
+ Z80_O_WR = 0;
+ Z80_O_WR = 0;
+ Z80_O_WR = 1;
+ }
+ Z80_O_MREQ = 1;
+}
+
+void z80_write_block(const __flash uint8_t *src, uint32_t dest, uint32_t length)
+{
+ uint8_t data;
+
+ z80_setup_dbus_out();
+ Z80_O_MREQ = 0;
+ while(length--) {
+ z80_setaddress(dest++);
+ data = *src++;
+ P_DB = data;
+ P_DB = data;
+ Z80_O_WR = 0;
+ Z80_O_WR = 0;
+ Z80_O_WR = 1;
+ }
+ Z80_O_MREQ = 1;
+}
+
+/*
+ 0179' rx.bs_mask: ds 1 ; (buf_len - 1)
+ 017A' rx.in_idx: ds 1 ;
+ 017B' rx.out_idx: ds 1 ;
+ 017C' rx.buf: ds rx.buf_len ;
+ 018B' rx.buf_end equ $-1 ; last byte (start+len-1)
+
+ 018C' tx.bs_mask: ds 1 ; (buf_len - 1)
+ 018D' tx.in_idx: ds 1 ;
+ 018E' tx.out_idx: ds 1 ;
+ 018F' tx.buf: ds tx.buf_len ;
+ 019E' tx.buf_end equ $-1 ; last byte
+*/
+
+
+typedef struct __attribute__((packed)) {
+ uint8_t mask;
+ uint8_t in_idx;
+ uint8_t out_idx;
+ uint8_t buf[];
+} zfifo_t;
+
+
+
+#define FIFO_BUFSIZE_MASK -3
+#define FIFO_INDEX_IN -2
+#define FIFO_INDEX_OUT -1
+
+
+static struct {
+ uint32_t base;
+ uint8_t idx_out,
+ idx_in,
+ mask;
+ } fifo_dsc[NUM_FIFOS];
+
+
+void z80_memfifo_init(const fifo_t f, uint32_t adr)
+{
+
+DBG_P(2, "z80_memfifo_init: %i, %lx\n", f, adr);
+
+ fifo_dsc[f].base = adr;
+
+ z80_bus_cmd(Request);
+
+ fifo_dsc[f].mask = z80_read(adr + FIFO_BUFSIZE_MASK);
+ fifo_dsc[f].idx_in = z80_read(adr + FIFO_INDEX_IN);
+ fifo_dsc[f].idx_out = z80_read(adr + FIFO_INDEX_OUT);
+
+ z80_bus_cmd(Release);
+}
+
+
+int z80_memfifo_is_empty(const fifo_t f)
+{
+ int rc = 1;
+
+ if (fifo_dsc[f].base != 0) {
+
+ uint32_t adr = fifo_dsc[f].base + FIFO_INDEX_IN;
+ uint8_t idx;
+
+ z80_bus_cmd(Request);
+ idx = z80_read(adr);
+ z80_bus_cmd(Release);
+ rc = idx == fifo_dsc[f].idx_out;
+ }
+
+ return rc;
+}
+
+int z80_memfifo_is_full(const fifo_t f)
+{
+ int rc = 1;
+
+ if (fifo_dsc[f].base != 0) {
+ z80_bus_cmd(Request);
+ rc = ((fifo_dsc[f].idx_in + 1) & fifo_dsc[f].mask)
+ == z80_read(fifo_dsc[f].base+FIFO_INDEX_OUT);
+ z80_bus_cmd(Release);
+ }
+ return rc;
+}
+
+uint8_t z80_memfifo_getc(const fifo_t f)
+{
+ uint8_t rc, idx;
+
+ while (z80_memfifo_is_empty(f))
+ ;
+
+ z80_bus_cmd(Request);
+ idx = fifo_dsc[f].idx_out;
+ rc = z80_read(fifo_dsc[f].base+idx);
+ fifo_dsc[f].idx_out = ++idx & fifo_dsc[f].mask;
+ z80_write(fifo_dsc[f].base+FIFO_INDEX_OUT, fifo_dsc[f].idx_out);
+ z80_bus_cmd(Release);
+
+ return rc;
+}
+
+
+void z80_memfifo_putc(fifo_t f, uint8_t val)
+{
+ int idx;
+
+ while (z80_memfifo_is_full(f))
+ ;
+
+ z80_bus_cmd(Request);
+ idx = fifo_dsc[f].idx_in;
+ z80_write(fifo_dsc[f].base+idx, val);
+ fifo_dsc[f].idx_in = ++idx & fifo_dsc[f].mask;
+ z80_write(fifo_dsc[f].base+FIFO_INDEX_IN, fifo_dsc[f].idx_in);
+ z80_bus_cmd(Release);
+}
+
+/*--------------------------------------------------------------------------*/
+/*
+ TODO: Rewrite msg_fifo routines for AVR
+*/
+
+static struct {
+ uint32_t base;
+ //uint8_t idx_out, idx_in;
+ uint16_t count;
+ uint8_t buf[256];
+ } msg_fifo;
+
+/*--------------------------------------------------------------------------*/
+
+#if 0
+
+static void tim1_setup(void)
+{
+ RCC_APB2RSTR |= RCC_APB2RSTR_TIM1RST;
+ RCC_APB2RSTR &= ~RCC_APB2RSTR_TIM1RST;
+
+ TIM1_CR1 = 0;
+
+ TIM1_SMCR = 0
+ /* | TIM_SMCR_ETP */
+ /* | TIM_SMCR_ETF_CK_INT_N_2 */
+ | TIM_SMCR_TS_ETRF
+ | TIM_SMCR_SMS_OFF
+ ;
+
+ TIM1_DIER = TIM_DIER_TDE;
+
+
+ TIM1_CCMR1 = 0
+ | TIM_CCMR1_OC1M_FORCE_LOW
+ | TIM_CCMR1_CC1S_OUT;
+
+ TIM1_SMCR |= TIM_SMCR_SMS_TM;
+}
+
+#endif
+
+/*--------------------------------------------------------------------------*/
+
+void z80_setup_msg_fifo(void)
+{
+// gpio_set_mode(P_BUSACK, GPIO_MODE_INPUT,
+// GPIO_CNF_INPUT_FLOAT, GPIO_BUSACK | GPIO_IOCS1);
+
+//...
+
+// msg_fifo.count = NELEMS(msg_fifo.buf);
+ msg_fifo.count = 0;
+ msg_fifo.base = 0;
+
+}
+
+
+void z80_init_msg_fifo(uint32_t addr)
+{
+
+DBG_P(1, "z80_init_msg_fifo: %lx\n", addr);
+
+ z80_bus_cmd(Request);
+ z80_write(addr+FIFO_INDEX_OUT, z80_read(addr+FIFO_INDEX_IN));
+ z80_bus_cmd(Release);
+ msg_fifo.base = addr;
+}
+
+
+int z80_msg_fifo_getc(void)
+{
+ int c = -1;
+
+#if 0
+ if (msg_fifo.count != (NELEMS(msg_fifo.buf) /*- DMA1_CNDTR4 */ )) {
+ c = msg_fifo.buf[msg_fifo.count];
+ if (++msg_fifo.count == NELEMS(msg_fifo.buf))
+ msg_fifo.count = 0;
+
+ if (msg_fifo.base != 0) {
+ z80_bus_cmd(Request);
+ z80_write(msg_fifo.base+FIFO_INDEX_OUT, msg_fifo.count);
+ z80_bus_cmd(Release);
+ }
+ }
+#endif
+
+ return c;
+}
diff --git a/avr/z80-if.h b/avr/z80-if.h
new file mode 100644
index 0000000..b02fe23
--- /dev/null
+++ b/avr/z80-if.h
@@ -0,0 +1,46 @@
+
+#define ZST_ACQUIRED 0x01
+#define ZST_RUNNING 0x02
+
+typedef enum {
+ RESET = 0x00,
+ RESET_AQRD = ZST_ACQUIRED,
+ RUNNING = ZST_RUNNING,
+ RUNNING_AQRD = ZST_RUNNING | ZST_ACQUIRED,
+} zstate_t;
+
+typedef enum {
+ Reset,
+ Request,
+ Release,
+ Run,
+ Restart,
+ M_Cycle
+} bus_cmd_t;
+
+typedef enum {LOW, HIGH} level_t;
+
+zstate_t z80_bus_state(void);
+zstate_t z80_bus_cmd(bus_cmd_t cmd);
+void z80_setup_bus(void);
+int z80_stat_reset(void);
+//void z80_busreq(level_t level);
+int z80_stat_halt(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);
+void z80_write_block(const FLASH uint8_t *src, uint32_t dest, uint32_t length);
+
+
+typedef enum fifo_t {fifo_in, fifo_out, NUM_FIFOS} fifo_t;
+
+void z80_memfifo_init(const fifo_t f, uint32_t adr);
+int z80_memfifo_is_empty(const fifo_t f);
+int z80_memfifo_is_full(const fifo_t f);
+uint8_t z80_memfifo_getc(const fifo_t f);
+void z80_memfifo_putc(fifo_t f, uint8_t val);
+
+void z80_setup_msg_fifo(void);
+void z80_init_msg_fifo(uint32_t addr);
+int z80_msg_fifo_getc(void);
diff --git a/configs/debug.config b/configs/debug.config
new file mode 100644
index 0000000..1a54f76
--- /dev/null
+++ b/configs/debug.config
@@ -0,0 +1 @@
+CONFIG_DEBUG=0
diff --git a/configs/default.config b/configs/default.config
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/configs/default.config
diff --git a/configs/gcc.tup b/configs/gcc.tup
new file mode 100644
index 0000000..ad2d788
--- /dev/null
+++ b/configs/gcc.tup
@@ -0,0 +1,40 @@
+CC = $(TOOLCHAIN)-gcc
+LD = $(TOOLCHAIN)-gcc
+AR = $(TOOLCHAIN)-ar
+AS = $(TOOLCHAIN)-as
+OBJCOPY = $(TOOLCHAIN)-objcopy
+OBJDUMP = $(TOOLCHAIN)-objdump
+SIZE = $(TOOLCHAIN)-size
+GDB = $(TOOLCHAIN)-gdb
+
+
+CFLAGS += -g -Os
+CFLAGS += -std=gnu99
+CFLAGS += -Wall -Wextra
+CFLAGS += -Wredundant-decls
+#CFLAGS += -fno-common -ffunction-sections -fdata-sections
+
+
+LDFLAGS += -Wl,--gc-sections
+LDFLAGS += -Wl,--cref
+
+ifneq ($(LDSCRIPT),)
+LDFLAGS += -T$(LDSCRIPT)
+endif
+
+
+!cc = |> ^ CC %f^ $(CC) $(CFLAGS) $(CPPFLAGS) -c %f -o %o |> %B.o
+!LINK = |> ^ LINK %o^ $(LD) $(CFLAGS) $(LDFLAGS) -Wl,-Map=%O.map %f $(LDLIBS) -o %o |> | %O.map
+!OBJCOPY= |> ^ OBJCOPY %o^ $(OBJCOPY) -Oihex %f %o |>
+!OBJDUMP= |> ^ OBJDUMP %o^ $(OBJDUMP) -h -S %f > %o |> %O.lss
+!SIZE = |> ^ SIZE %f^ $(SIZE) %f |>
+
+
+: foreach $(SRC) | $(PREDEP) |> !cc |> {objs}
+: $(SRC_Z) |> !cc $(CPPFLAGS_Z) |> {objs}
+
+: {objs} |> !LINK |> $(PROG).elf {elf}
+: {elf} |> !OBJCOPY |> %B.hex {aux}
+: {elf} |> !OBJDUMP |> %B.lss {aux}
+: {elf} | {aux} |> !SIZE |>
+
diff --git a/libopencm3 b/libopencm3
new file mode 160000
+Subproject 69726cd1a5baf455d247d3e2469e53cd26856c2
diff --git a/stm32/Tupfile b/stm32/Tupfile
new file mode 100644
index 0000000..1778623
--- /dev/null
+++ b/stm32/Tupfile
@@ -0,0 +1,74 @@
+include_rules
+
+PROG = z180-stamp-stm32
+SRC = z180-stamp-stm32.c serial.c z80-if.c
+SRC_Z = ../z180/hdrom.c
+#TARGETS = $(PROG).elf
+
+FP_FLAGS = -msoft-float
+DEFS = -DSTM32F1 -DBAUD=115200
+
+LIBNAME = opencm3_stm32f1
+OPENCM3_DIR = $(TOP)/libopencm3
+LDSCRIPT = stm32vl-discovery.ld
+
+###############################################################################
+# Executables
+
+TOOLCHAINDIR = /usr/local/gcc-arm-none-eabi-4_8-2014q2/bin
+TOOLCHAIN = $(TOOLCHAINDIR)/arm-none-eabi
+
+CC = $(TOOLCHAIN)-gcc
+LD = $(TOOLCHAIN)-gcc
+AR = $(TOOLCHAIN)-ar
+AS = $(TOOLCHAIN)-as
+OBJCOPY = $(TOOLCHAIN)-objcopy
+OBJDUMP = $(TOOLCHAIN)-objdump
+SIZE = $(TOOLCHAIN)-size
+GDB = $(TOOLCHAIN)-gdb
+
+###############################################################################
+
+INCLUDES += $(OPENCM3_DIR)/include
+LIBDIRS += $(OPENCM3_DIR)/lib
+
+SCRIPT_DIR = $(OPENCM3_DIR)/scripts
+
+
+ifdef DEBUG
+DEFS += -DDEBUG=2
+endif
+
+CFLAGS += -g -Os
+CFLAGS += -mthumb -mcpu=cortex-m3 $(FP_FLAGS) -mfix-cortex-m3-ldrd
+CFLAGS += -std=gnu99
+CFLAGS += -Wall -Wextra -Wimplicit-function-declaration
+CFLAGS += -Wredundant-decls
+#CFLAGS += -fno-common -ffunction-sections -fdata-sections
+CFLAGS += -I $(INCLUDES)
+
+CPPFLAGS += $(DEFS)
+
+# Linker flags
+LDFLAGS += --static -nostartfiles
+LDFLAGS += -T$(LDSCRIPT)
+LDFLAGS += -Wl,--gc-sections
+LDFLAGS += -Wl,--cref
+
+LDLIBS += -L$(LIBDIRS) -l$(LIBNAME)
+LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
+
+
+!cc = |> ^ CC %f^ $(CC) $(CFLAGS) $(CPPFLAGS) -c %f -o %o |> %B.o
+!LINK = |> ^ LINK %o^ $(LD) $(CFLAGS) $(LDFLAGS) -Wl,-Map=%O.map %f $(LDLIBS) -o %o |> | %O.map
+!OBJCOPY= |> ^ OBJCOPY %o^ $(OBJCOPY) -Oihex %f %o |>
+!OBJDUMP= |> ^ OBJDUMP %o^ $(OBJDUMP) -h -S %f > %o |> %O.lss
+!SIZE = |> ^ SIZE^ $(SIZE) %f |>
+
+: foreach $(SRC) $(SRC_Z) | ../z180/hdrom.h |> !cc |> {objs}
+
+: {objs} |> !LINK |> $(PROG).elf
+: $(PROG).elf |> !OBJCOPY |> %B.hex
+: $(PROG).elf |> !OBJDUMP |> %B.lss
+: $(PROG).elf |> !SIZE |>
+
diff --git a/stm32/debug.h b/stm32/debug.h
new file mode 100644
index 0000000..d542317
--- /dev/null
+++ b/stm32/debug.h
@@ -0,0 +1,11 @@
+
+#ifndef DEBUG_H_
+#define DEBUG_H_
+
+#ifdef DEBUG
+#define DBG_P(lvl, ...) if (DEBUG>=lvl) fprintf( stderr, __VA_ARGS__ )
+#else
+#define DBG_P(lvl, ...)
+#endif
+
+#endif /* DEBUG_H_ */
diff --git a/stm32/serial.c b/stm32/serial.c
new file mode 100644
index 0000000..9193005
--- /dev/null
+++ b/stm32/serial.c
@@ -0,0 +1,194 @@
+/*
+ * serial.c
+ *
+ * Created on: 04.05.2014
+ * Author: leo
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/usart.h>
+#include <libopencm3/cm3/nvic.h>
+#include "serial.h"
+
+
+#define USART_CONSOLE USART1
+
+int _write(int fd, char *ptr, int len) __attribute__((used));
+
+
+struct ring {
+ uint8_t *data;
+ int size;
+ volatile int begin;
+ volatile int end;
+};
+
+
+#define BUFFER_SIZE 256
+
+struct ring rx_ring;
+struct ring tx_ring;
+uint8_t rx_ring_buffer[BUFFER_SIZE];
+uint8_t tx_ring_buffer[BUFFER_SIZE];
+
+
+static void ring_init(struct ring *ring, uint8_t *buf, int size)
+{
+ ring->data = buf;
+ ring->size = size;
+ ring->begin = 0;
+ ring->end = 0;
+}
+
+static int ring_write_ch(struct ring *ring, uint8_t ch)
+{
+ int ep = (ring->end + 1) % ring->size;
+
+ if ((ep) != ring->begin) {
+ ring->data[ring->end] = ch;
+ ring->end = ep;
+ return 1;
+ }
+
+ return -1;
+}
+
+static int ring_write(struct ring *ring, uint8_t *data, int size)
+{
+ int i;
+
+ for (i = 0; i < size; i++) {
+ if (ring_write_ch(ring, data[i]) < 0)
+ return -i;
+ }
+
+ return i;
+}
+
+static int ring_read_ch(struct ring *ring)
+{
+ int ret = -1;
+
+ if (ring->begin != ring->end) {
+ ret = ring->data[ring->begin];
+ ring->begin = (ring->begin +1) % ring->size;
+ }
+
+ return ret;
+}
+
+void usart_setup(void)
+{
+ /* Initialize output ring buffer. */
+ ring_init(&rx_ring, rx_ring_buffer, BUFFER_SIZE);
+ ring_init(&tx_ring, tx_ring_buffer, BUFFER_SIZE);
+
+ /* Enable the USART1 interrupt. */
+ nvic_enable_irq(NVIC_USART1_IRQ);
+
+ /* Setup GPIO pin GPIO_USART1_TX/LED_GREEN_PIN on GPIO port A for transmit. */
+ /* TODO: USART1 --> USART_CONSOLE */
+
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
+
+ /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */
+ gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
+
+ /* Setup UART parameters. */
+// usart_set_baudrate(USART_CONSOLE, 38400);
+ usart_set_baudrate(USART_CONSOLE, 115200);
+ usart_set_databits(USART_CONSOLE, 8);
+ usart_set_stopbits(USART_CONSOLE, USART_STOPBITS_1);
+ usart_set_parity(USART_CONSOLE, USART_PARITY_NONE);
+ usart_set_flow_control(USART_CONSOLE, USART_FLOWCONTROL_NONE);
+ usart_set_mode(USART_CONSOLE, USART_MODE_TX_RX);
+
+ /* Enable USART1 Receive interrupt. */
+ USART_CR1(USART1) |= USART_CR1_RXNEIE;
+
+ /* Finally enable the USART. */
+ usart_enable(USART_CONSOLE);
+}
+
+void usart1_isr(void)
+{
+ /* Check if we were called because of RXNE. */
+ if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_RXNE) != 0)) {
+
+ /* Retrieve the data from the peripheral. */
+ ring_write_ch(&rx_ring, usart_recv(USART1));
+
+ }
+
+ /* Check if we were called because of TXE. */
+ if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_TXE) != 0)) {
+
+ int data;
+
+ data = ring_read_ch(&tx_ring);
+
+ if (data == -1) {
+ /* Disable the TXE interrupt, it's no longer needed. */
+ USART_CR1(USART1) &= ~USART_CR1_TXEIE;
+ } else {
+ /* Put data into the transmit register. */
+ usart_send(USART1, data);
+ }
+ }
+}
+
+/*--------------------------------------------------------------------------*/
+
+void serial_setup(void)
+{
+ usart_setup();
+}
+
+/*--------------------------------------------------------------------------*/
+
+/**
+ * Use USART_CONSOLE as a console.
+ * This is a syscall for newlib
+ * @param fd
+ * @param ptr
+ * @param len
+ * @return
+ */
+int _write(int fd, char *ptr, int len)
+{
+ int i;
+
+ if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
+ for (i = 0; i < len; i++) {
+ if (ptr[i] == '\n') {
+ serial_putc('\r');
+ }
+ serial_putc(ptr[i]);
+ }
+ return i;
+ }
+ errno = EIO;
+ return -1;
+}
+
+int serial_getc(void)
+{
+ return ring_read_ch(&rx_ring);
+}
+
+void serial_putc(uint8_t data)
+{
+ while (ring_write_ch(&tx_ring, data) < 0)
+ ;
+
+ /* Enable the TXE interrupt. */
+ USART_CR1(USART1) |= USART_CR1_TXEIE;
+}
diff --git a/stm32/serial.h b/stm32/serial.h
new file mode 100644
index 0000000..1a0f510
--- /dev/null
+++ b/stm32/serial.h
@@ -0,0 +1,8 @@
+#ifndef SERIAL_H
+#define SERIAL_H
+
+void serial_setup(void);
+void serial_putc(uint8_t);
+int serial_getc(void);
+
+#endif /* SERIAL_H */
diff --git a/stm32/stm32vl-discovery.ld b/stm32/stm32vl-discovery.ld
new file mode 100644
index 0000000..40de3d3
--- /dev/null
+++ b/stm32/stm32vl-discovery.ld
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Linker script for ST STM32VLDISCOVERY (STM32F100RB, 128K flash, 8K RAM). */
+
+/* Define memory regions. */
+MEMORY
+{
+ rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 8K
+}
+
+/* Include the common ld script. */
+INCLUDE libopencm3_stm32f1.ld
+
diff --git a/stm32/z180-stamp-stm32.c b/stm32/z180-stamp-stm32.c
new file mode 100644
index 0000000..15d732d
--- /dev/null
+++ b/stm32/z180-stamp-stm32.c
@@ -0,0 +1,763 @@
+/*
+ */
+
+#include <stdio.h>
+
+#include <libopencmsis/core_cm3.h>
+#include <libopencm3/cm3/nvic.h>
+#include <libopencm3/cm3/systick.h>
+#include <libopencm3/stm32/rtc.h>
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/timer.h>
+
+#define ODR 0x0c
+#define IDR 0x08
+
+
+#include "debug.h"
+#include "serial.h"
+#include "z80-if.h"
+#include "../z180/hdrom.h"
+
+#define ESCCHAR ('^'-0x40)
+
+#define S_10MS_TO (1<<0)
+
+/*
+ * LED Connections
+ */
+
+#define LED_PORT GPIOC
+#define LED_BLUE_PIN GPIO8
+#define BLUE 8
+#define LED_GREEN_PIN GPIO9
+#define GREEN 9
+
+
+#define LED_BLUE_ON() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 1
+#define LED_BLUE_OFF() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 0
+#define LED_BLUE_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, BLUE) = !BBIO_PERIPH(LED_PORT+ODR, BLUE)
+
+#define LED_GREEN_ON() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 1
+#define LED_GREEN_OFF() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 0
+#define LED_GREEN_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, GREEN) = !BBIO_PERIPH(LED_PORT+ODR, GREEN)
+
+
+/*
+ * Button connections
+ */
+
+//BBIO_PERIPH(GPIOA+IDR, 0);
+
+#define KEY_PORT GPIOA_IDR
+#define KEY0 GPIO0
+//#define KEY1 GPIO1
+//#define KEY2 GPIO2
+
+#define REPEAT_MASK KEY0 // repeat: key0
+#define REPEAT_START 100 // after 1s
+#define REPEAT_NEXT 20 // every 200ms
+
+
+typedef enum {
+ NOTHING, PULSE, BLINK1, BLINK2
+} LED_MODE;
+
+typedef struct {
+ uint8_t mode;
+ uint8_t ontime, offtime;
+} led_stat_t;
+
+volatile uint8_t led_timer[2];
+led_stat_t led_stat[2];
+
+volatile int timeout_1s;
+volatile uint32_t Stat;
+
+
+/*--------------------------------------------------------------------------*/
+
+
+static void clock_setup(void)
+{
+ //rcc_clock_setup_in_hse_8mhz_out_24mhz();
+ rcc_clock_setup_in_hsi_out_24mhz();
+
+ /* Enable clocks for:
+ GPIO port A (for GPIO_USART1_TX and Button)
+ GPIO port C (LEDs)
+ USART1
+ TIM16 (RST-Pin)
+ TIM1 (IOCS1)
+ */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR,
+ RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN
+ | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN
+ | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN
+ | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_TIM16EN);
+ /* Enable clocks for:
+ TIM3
+ */
+ rcc_peripheral_enable_clock(&RCC_APB1ENR,
+ RCC_APB1ENR_TIM3EN);
+
+ /* Enable clocks for:
+ DMA1
+ */
+ rcc_peripheral_enable_clock(&RCC_AHBENR,
+ RCC_AHBENR_DMA1EN);
+}
+
+static void systick_setup(void)
+{
+ /* SysTick interrupt every N clock pulses: set reload to N-1 */
+ STK_RVR = 24000000/1000 - 1;
+
+ /* Set source to core clock, enable int and start counting. */
+ STK_CSR = STK_CSR_CLKSOURCE_AHB | STK_CSR_TICKINT | STK_CSR_ENABLE;
+}
+
+#if 0
+static void nvic_setup(void)
+{
+// nvic_enable_irq(NVIC_RTC_IRQ);
+// nvic_set_priority(NVIC_RTC_IRQ, 1);
+}
+#endif
+
+static void tim3_setup(void)
+{
+ TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP;
+
+ TIM3_CCMR2 = 0
+ | TIM_CCMR2_OC4M_FORCE_LOW
+ /* | TIM_CCMR2_OC4M_FORCE_HIGH */
+ /* | TIM_CCMR2_OC4M_PWM2 */
+
+ /* | TIM_CCMR2_OC4PE */
+ /* | TIM_CCMR2_OC4FE */
+ | TIM_CCMR2_CC4S_OUT;
+
+ TIM3_CCER = TIM_CCER_CC4E
+ | TIM_CCER_CC4P;
+
+ TIM3_ARR = 48; /* default */
+ TIM3_CCR4 = 1; /* */
+}
+
+static void gpio_setup(void)
+{
+
+ /* Disable JTAG-DP, but leave SW-DP Enabled. (free PA15, PB3, PB4)
+ Remap SPI1 to PB3, PB4, PB5 and PA15.
+ Remap TIM3 (CH1/PC6, CH2/PC7, CH3/PC8, CH4/PC9)
+ Port D0/Port D1 mapping on OSC_IN/OSC_OUT
+ */
+ gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON,
+ AFIO_MAPR_SPI1_REMAP
+ | AFIO_MAPR_TIM3_REMAP_FULL_REMAP
+ | AFIO_MAPR_PD01_REMAP);
+
+ /* LEDs and User Button. */
+ gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, LED_BLUE_PIN);
+ gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LED_GREEN_PIN);
+ gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_FLOAT, GPIO0);
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+void delay_systicks(int ticks)
+{
+ int start, stop, now;
+
+ start = STK_CVR;
+ stop = start - ticks;
+ if (stop < 0) {
+ stop += STK_RVR;
+ do {
+ now = STK_CVR;
+ } while ((now > stop) || (now <= start));
+ } else {
+ do {
+ now = STK_CVR;
+ } while ((now > stop) && (now <= start));
+ }
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+static void led_toggle(uint8_t lednr) {
+ if (lednr == 0)
+ LED_BLUE_TOGGLE();
+ else if (lednr == 1)
+ LED_GREEN_TOGGLE();
+}
+
+static void led_on(uint8_t lednr) {
+ if (lednr == 0)
+ LED_BLUE_ON();
+ else if (lednr == 1)
+ LED_GREEN_ON();
+}
+
+static void led_off(uint8_t lednr) {
+ if (lednr == 0)
+ LED_BLUE_OFF();
+ else if (lednr == 1)
+ LED_GREEN_OFF();
+}
+
+static uint8_t led_is_on(uint8_t lednr) {
+ if (lednr == 0)
+ return BBIO_PERIPH(LED_PORT+ODR, BLUE);
+ else if (lednr == 1)
+ return BBIO_PERIPH(LED_PORT+ODR, GREEN);
+ else
+ return 0;
+}
+
+static void ledset(uint8_t lednr, uint8_t what, uint8_t len) {
+
+ led_stat[lednr].mode = what;
+ switch (what) {
+ case PULSE:
+ led_stat[lednr].ontime = len;
+ led_stat[lednr].offtime = 0;
+ led_timer[lednr] = len;
+ led_on(lednr);
+ break;
+ case BLINK1:
+ case BLINK2:
+ if (what == BLINK1)
+ led_stat[lednr].offtime = 100 - len;
+ else
+ led_stat[lednr].offtime = 200 - len;
+ led_stat[lednr].ontime = len;
+ led_timer[lednr] = len;
+ led_on(lednr);
+ break;
+ default:
+ break;
+ }
+}
+
+/*--------------------------------------------------------------------------*/
+
+static volatile uint16_t key_state,
+ key_press, // key press detect
+ key_rpt; // key long press and repeat
+
+
+static uint16_t get_key_press(uint16_t key_mask) {
+ __disable_irq();
+ // read and clear atomic !
+ key_mask &= key_press; // read key(s)
+ key_press ^= key_mask; // clear key(s)
+ __enable_irq();
+ return key_mask;
+}
+
+/*
+static uint16_t get_key_rpt(uint16_t key_mask) {
+ __disable_irq();
+ // read and clear atomic !
+ key_mask &= key_rpt; // read key(s)
+ key_rpt ^= key_mask; // clear key(s)
+ __enable_irq();
+ return key_mask;
+}
+*/
+
+static uint16_t get_key_short(uint16_t key_mask) {
+ __disable_irq();
+ // read key state and key press atomic !
+ return get_key_press(key_state & key_mask);
+}
+
+/*
+static uint16_t get_key_long(uint16_t key_mask) {
+ return get_key_press(get_key_rpt(key_mask));
+}
+*/
+
+static void key_timerproc() {
+ static uint16_t key_in_last, rpt;
+ uint16_t key_in, c;
+
+ key_in = KEY_PORT;
+
+ c = key_in_last & key_in & ~key_state;
+
+// key_state = key_state & key_in_last | (key_state | key_in_last) & key_in;
+// key_state = key_state & key_in | (key_state | key_in) & key_in_last;
+
+ key_state = c | ((key_in_last | key_in) & key_state);
+
+// key_state = (key_state&key_in_last) | (key_state&key_in) | (key_in_last&key_in);
+
+ key_press |= c;
+
+ key_in_last = key_in;
+
+
+ if ((key_state & REPEAT_MASK) == 0) // check repeat function
+ rpt = REPEAT_START;
+ if (--rpt == 0) {
+ rpt = REPEAT_NEXT; // repeat delay
+ key_rpt |= key_state & REPEAT_MASK;
+ }
+
+}
+
+/*--------------------------------------------------------------------------*/
+
+void sys_tick_handler(void)
+{
+ static int_fast8_t tick_10ms = 0;
+ static int_fast16_t count_ms = 0;
+
+ int_fast8_t i;
+
+ ++tick_10ms;
+ if (tick_10ms == 10)
+ {
+ Stat |= S_10MS_TO;
+
+ tick_10ms = 0;
+
+ i = led_timer[0];
+ if (i)
+ led_timer[0] = i - 1;
+ i = led_timer[1];
+ if (i)
+ led_timer[1] = i - 1;
+
+ key_timerproc();
+
+ /* Drive timer procedure of low level disk I/O module */
+ //disk_timerproc();
+ }
+
+ count_ms++;
+ if (count_ms == 1000) {
+ count_ms = 0;
+
+ i = timeout_1s;
+ if (i)
+ timeout_1s = i - 1;
+ }
+}
+
+void rtc_isr(void)
+{
+ /* The interrupt flag isn't cleared by hardware, we have to do it. */
+ rtc_clear_flag(RTC_SEC);
+
+}
+
+/*--------------------------------------------------------------------------*/
+
+void tim3_set(int mode)
+{
+ uint16_t cc_mode;
+
+ cc_mode = TIM_CCMR2_CC4S_OUT;
+
+ TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP /*| TIM_CR1_OPM */ ;
+
+ if (mode < 0)
+ cc_mode |= TIM_CCMR2_OC4M_FORCE_LOW;
+ else if (mode == 0)
+ cc_mode |= TIM_CCMR2_OC4M_FORCE_HIGH;
+ else {
+ TIM3_ARR = mode;
+ TIM3_CCR4 = mode/2;
+ cc_mode |= TIM_CCMR2_OC4M_PWM2;
+ }
+
+ TIM3_CCMR2 = cc_mode;
+
+ if (mode > 0)
+ TIM3_CR1 |= TIM_CR1_CEN;
+}
+
+/*--------------------------------------------------------------------------*/
+
+static uint32_t z80_sram_cmp(uint32_t addr, uint32_t length, uint8_t wval, int inc)
+{
+ uint8_t rval;
+ int_fast8_t errors = 0;
+
+ DBG_P(1, "SRAM: Check %#.5x byte... ", length);
+ while (length--) {
+ if ((rval = z80_read(addr)) != wval) {
+ if (errors == 0) {
+ printf("\nSRAM: Address W R\n" \
+ " -------------\n");
+// 12345 00 11
+ }
+ printf(" %.5lx %.2x %.2x\n", addr, wval, rval);
+
+ if (++errors > 16 )
+ break;
+ }
+ addr++;
+ wval += inc;
+ }
+ DBG_P(1, "Done.\n");
+
+ return addr;
+}
+
+#if 0
+static void z80_sram_fill(uint32_t addr, int length, uint8_t startval, int inc)
+{
+ printf("SRAM: Write %#.5x byte... ", length); //fflush(stdout);
+ while (length--) {
+ z80_write(addr, startval);
+ ++addr;
+ startval += inc;
+ }
+ printf("Done.\n");
+}
+
+
+void z80_sram_fill_string(uint32_t addr, int length, const char *text)
+{
+ char c;
+ const char *p = text;
+
+ while (length--) {
+ z80_write(addr++, c = *p++);
+ if (c == 0)
+ p = text;
+ }
+}
+
+
+uint32_t z80_sram_cmp_string(uint32_t addr, int length, const char *text)
+{
+ char c;
+ const char *p = text;
+
+ while (length--) {
+ c = *p++;
+ if (z80_read(addr) != c)
+ break;
+ ++addr;
+ if (c == 0)
+ p = text;
+ }
+ return addr;
+}
+
+const char * const qbfox = "Zhe quick brown fox jumps over the lazy dog!";
+const char * const qbcat = "Zhe quick brown fox jumps over the lazy cat!";
+
+#endif
+
+uint8_t z80_get_byte(uint32_t adr)
+{
+ uint8_t data;
+
+ z80_request_bus();
+ data = z80_read(adr),
+ z80_release_bus();
+
+ return data;
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+static void do_10ms(void)
+{
+ for (uint_fast8_t i = 0; i < 2; i++) {
+ switch (led_stat[i].mode) {
+ case PULSE:
+ if (led_timer[i] == 0) {
+ led_off(i);
+ led_stat[i].mode = NOTHING;
+ }
+ break;
+ case BLINK1:
+ case BLINK2:
+ if (led_timer[i] == 0) {
+ if (led_is_on(i))
+ led_timer[i] = led_stat[i].offtime;
+ else
+ led_timer[i] = led_stat[i].ontime;
+ led_toggle(i);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+struct msg_item {
+ uint8_t fct;
+ uint8_t sub_min, sub_max;
+ void (*func)(uint8_t, int, uint8_t *);
+};
+
+uint32_t msg_to_addr(uint8_t *msg)
+{
+ uint32_t addr = msg[0] | (msg[1] << 8) | ((uint32_t)msg[2] << 16);
+
+ return addr;
+
+}
+
+void do_msg_ini_msgfifo(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)subf; (void)len;
+
+ z80_init_msg_fifo(msg_to_addr(msg));
+}
+
+
+void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)len;
+
+ z80_memfifo_init(subf - 1, msg_to_addr(msg));
+}
+
+
+void do_msg_char_out(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)subf;
+
+ while (len--)
+ putchar(*msg++);
+}
+
+
+const struct msg_item z80_messages[] =
+{
+ { 0, /* fct nr. */
+ 0, 0, /* sub fct nr. from, to */
+ &do_msg_ini_msgfifo},
+ { 0,
+ 1, 2,
+ &do_msg_ini_memfifo},
+ { 1,
+ 1, 1,
+ &do_msg_char_out},
+ { 0xff, /* end mark */
+ 0, 0,
+ 0},
+
+};
+
+
+
+
+void do_message(int len, uint8_t *msg)
+{
+ uint8_t fct, sub_fct;
+ int_fast8_t i = 0;
+
+ if (len >= 2) {
+ fct = *msg++;
+ sub_fct = *msg++;
+ len -= 2;
+
+ while (fct != z80_messages[i].fct)
+ ++i;
+
+ if (z80_messages[i].fct == 0xff) {
+ DBG_P(1, "do_message: Unknown function: %i, %i\n",
+ fct, sub_fct);
+ return; /* TODO: unknown message # */
+ }
+
+ while (fct == z80_messages[i].fct) {
+ if (sub_fct >= z80_messages[i].sub_min && sub_fct <= z80_messages[i].sub_max )
+ break;
+ ++i;
+ }
+
+ if (z80_messages[i].fct != fct) {
+ DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
+ fct, sub_fct);
+ return; /* TODO: unknown message sub# */
+ }
+
+ (z80_messages[i].func)(sub_fct, len, msg);
+
+
+ } else {
+ /* TODO: error */
+ DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len);
+ }
+}
+
+
+
+#define CTRBUF_LEN 256
+
+void check_msg_fifo(void)
+{
+ int ch;
+ static int_fast8_t state;
+ static int msglen,idx;
+ static uint8_t buffer[CTRBUF_LEN];
+
+ while (state != 3 && (ch = z80_msg_fifo_getc()) >= 0) {
+ switch (state) {
+ case 0: /* wait for start of message */
+ if (ch == 0x81) {
+ msglen = 0;
+ idx = 0;
+ state = 1;
+ }
+ break;
+ case 1: /* get msg len */
+ if (ch > 0 && ch <= CTRBUF_LEN) {
+ msglen = ch;
+ state = 2;
+ } else
+ state = 0;
+ break;
+ case 2: /* get message */
+ buffer[idx++] = ch;
+ if (idx == msglen)
+ state = 3;
+ break;
+ }
+ }
+
+ if (state == 3) {
+ do_message(msglen, buffer);
+ state = 0;
+ }
+}
+
+
+void z80_load_mem(void)
+{
+ unsigned sec = 0;
+ uint32_t sec_base = hdrom_start;
+
+ DBG_P(1, "Loading z80 memory... \n");
+
+ while (sec < hdrom_sections) {
+ DBG_P(2, " From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n",
+ hdrom_address[sec],
+ hdrom_address[sec]+hdrom_length_of_sections[sec] - 1,
+ hdrom_length_of_sections[sec]);
+
+ z80_write_block((unsigned char *) &hdrom[sec_base], /* src */
+ hdrom_address[sec], /* dest */
+ hdrom_length_of_sections[sec]); /* len */
+ sec_base+=hdrom_length_of_sections[sec];
+ sec++;
+ }
+}
+/*--------------------------------------------------------------------------*/
+
+int main(void)
+{
+ int_fast8_t state = 0;
+ int ch;
+
+ clock_setup();
+ gpio_setup();
+ tim3_setup();
+ setvbuf(stdout, NULL, _IONBF, 0);
+ serial_setup();
+ printf("\n(STM32F100+HD64180)_stamp Tester\n");
+
+ DBG_P(1, "z80_setup_bus... ");
+ z80_setup_msg_fifo();
+ z80_setup_bus();
+ DBG_P(1, "done.\n");
+
+ /*
+ * If the RTC is pre-configured just allow access, don't reconfigure.
+ * Otherwise enable it with the LSE as clock source and 0x7fff as
+ * prescale value.
+ */
+ rtc_auto_awake(LSE, 0x7fff);
+
+ systick_setup();
+
+ DBG_P(1, "Get bus... ");
+ z80_busreq(LOW);
+ z80_reset(HIGH);
+ z80_request_bus();
+ DBG_P(1, "got it!\n");
+
+ z80_memset(0, 0x76, 0x80000);
+ //z80_sram_fill(0, 512 * 1024, 0x76, 0);
+ z80_sram_cmp(0, (uint32_t)512 * 1024, 0x76, 0);
+
+ z80_load_mem();
+ z80_reset(LOW);
+ DBG_P(1, "Bus released!\n");
+ z80_release_bus();
+ z80_reset(HIGH);
+ DBG_P(1, "Reset released!\n");
+
+
+ ledset(0, BLINK1, 50);
+
+ while (1) {
+
+ if (Stat & S_10MS_TO) {
+ Stat &= ~S_10MS_TO;
+ do_10ms();
+ }
+
+ if (get_key_short(KEY0)) {
+ z80_reset_pulse();
+ }
+
+ if ((ch = serial_getc()) >= 0) {
+ switch (state) {
+ case 0:
+ if (ch == ESCCHAR) {
+ state = 1;
+ /* TODO: Timer starten */
+ } else
+ z80_memfifo_putc(fifo_out, ch);
+ break;
+ case 1:
+ switch (ch) {
+
+ case 'h': /* test: green led on */
+ tim3_set(-1);
+ break;
+ case 'l': /* test: green led off */
+ tim3_set(0);
+ break;
+ case 'p': /* test: pulse on led pin */
+ tim3_set(24000000 / 1000000 * 5); /* 5 us */
+ break;
+ case 'r':
+ z80_reset_pulse();
+ break;
+
+ case ESCCHAR:
+ default:
+ z80_memfifo_putc(fifo_out, ch);
+ }
+ state = 0;
+ break;
+ }
+ }
+
+ check_msg_fifo();
+ }
+
+ return 0;
+}
diff --git a/stm32/z80-if.c b/stm32/z80-if.c
new file mode 100644
index 0000000..171fea9
--- /dev/null
+++ b/stm32/z80-if.c
@@ -0,0 +1,731 @@
+/**
+ *
+ * Pin assignments
+ *
+ * | Z180-Sig | STM32-Port |Buffer | Dir | Special Function |
+ * +------------+---------------+-------+-------+-----------------------+
+ * | A0 | A 1 | P | O | |
+ * | A1 | A 2 | P | O | |
+ * | A2 | A 3 | P | O | |
+ * | A3 | A 4 | P | O | |
+ * | A4 | A 5 | P | O | |
+ * | A5 | A 6 | P | O | |
+ * | A6 | A 7 | P | O | |
+ * | A7 | A 8 | | O | |
+ * | A8 | C 0 | P | O | |
+ * | A9 | C 1 | P | O | |
+ * | A10 | C 2 | P | O | |
+ * | A11 | C 3 | P | O | |
+ * | A12 | C 4 | P | O | |
+ * | A13 | C 5 | P | O | |
+ * | A14 | C 6 | | O | |
+ * | A15 | C 7 | | O | |
+ * | A16 | C 10 | | O | |
+ * | A17 | C 11 | | O | |
+ * | A18 | C 12 | | O | |
+ * | D0 | B 8 | | I/O | |
+ * | D1 | B 9 | | I/O | |
+ * | D2 | B 10 | | I/O | |
+ * | D3 | B 11 | | I/O | |
+ * | D4 | B 12 | | I/O | |
+ * | D5 | B 13 | | I/O | |
+ * | D6 | B 14 | | I/O | |
+ * | D7 | B 15 | | I/O | |
+ * | ME | C 13 | P | O | |
+ * | RD | B 0 | P | O | |
+ * | WR | B 1 | P | O | |
+ * | BUSREQ | D 2 | | O | |
+ * | IOCS1 | A 11 | | I | TIM1_CH4 |
+ * | BUSACK | A 12 | | I | |
+ * | HALT | A 12 | | I | |
+ * | NMI | B 7 | | O | |
+ * | RST | B 6 | | O | TIM16_CH1N |
+ * | | | | | |
+ * | | A 9 | | | af1 USART1_TX |
+ * | | A 10 | | | af1 USART1_RX |
+ * | | A 15 | | JTDI | remap SPI1_NSS' |
+ * | | B 3 | | JTDO | remap SPI1_SCK' |
+ * | | B 4 | |NJTRST | remap SPI1_MISO' |
+ * | | B 5 | | | remap SPI1_MOSI' |
+ * | | C 14 | | | af1 OSC32 |
+ * | | C 15 | | | af1 OSC32 |
+
+
+AFIO_MAPR2 =
+AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON (frees
+AFIO_MAPR_SPI1_REMAP
+
+ */
+
+/**
+ *
+ * Pin assignments
+ *
+ * | Z180-Sig | STM32-Port | Buffer | Dir |Special Function |
+ * | -------- | ---------- | ------ | --- | --------------- |
+ * | A0 |A 1 |P |O | |
+ * | A1 |A 2 |P |O | |
+ * | A2 |A 3 |P |O | |
+ * | A3 |A 4 |P |O | |
+ * | A4 |A 5 |P |O | |
+ * | A5 |A 6 |P |O | |
+ * | A6 |A 7 |P |O | |
+ * | A7 |A 8 | |O | |
+ * | A8 |C 0 |P |O | |
+ * | A9 |C 1 |P |O | |
+ * | A10 |C 2 |P |O | |
+ * | A11 |C 3 |P |O | |
+ * | A12 |C 4 |P |O | |
+ * | A13 |C 5 |P |O | |
+ * | A14 |C 6 | |O | |
+ * | A15 |C 7 | |O | |
+ * | A16 |C 10 | |O | |
+ * | A17 |C 11 | |O | |
+ * | A18 |C 12 | |O | |
+ * | D0 |B 8 | |I/O | |
+ * | D1 |B 9 | |I/O | |
+ * | D2 |B 10 | |I/O | |
+ * | D3 |B 11 | |I/O | |
+ * | D4 |B 12 | |I/O | |
+ * | D5 |B 13 | |I/O | |
+ * | D6 |B 14 | |I/O | |
+ * | D7 |B 15 | |I/O | |
+ * | ME |C 13 |P |O | |
+ * | RD |B 0 |P |O | |
+ * | WR |B 1 |P |O | |
+ * | BUSREQ |D 2 | |O | |
+ * | IOCS1 |A 11 | |I |TIM1_CH4 |
+ * | BUSACK |A 12 | |I | |
+ * | HALT |A 12 | |I | |
+ * | NMI |B 7 | |O | |
+ * | RST |B 6 | |O |TIM16_CH1N |
+ * | | | | | |
+ * | |A 9 | | |af1 USART1_TX |
+ * | |A 10 | | |af1 USART1_RX |
+ * | |A 15 | |JTDI | remap SPI1_NSS' |
+ * | |B 3 | |JTDO |remap SPI1_SCK' |
+ * | |B 4 | |NJTRST |remap SPI1_MISO' |
+ * | |B 5 | | |remap SPI1_MOSI' |
+ * | |C 14 | | |af1 OSC32 |
+ * | |C 15 | | |af1 OSC32 |
+
+
+AFIO_MAPR2 =
+AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON (frees
+AFIO_MAPR_SPI1_REMAP
+
+ */
+
+#include <stdio.h>
+
+
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/timer.h>
+#include <libopencm3/stm32/dma.h>
+#include "debug.h"
+#include "z80-if.h"
+
+
+/* Number of array elements */
+#define NELEMS(x) (sizeof x/sizeof *x)
+
+#define ODR 0x0c
+#define IDR 0x08
+
+#define CONCAT(x,y) x ## y
+#define EVALUATOR(x,y) CONCAT(x,y)
+
+#define GPIO_(X) CONCAT(GPIO, X)
+
+
+
+
+#define P_ME GPIOC
+#define ME 13
+#define P_RD GPIOB
+#define RD 0
+#define P_WR GPIOB
+#define WR 1
+#define P_BUSREQ GPIOD
+#define BUSREQ 2
+#define P_BUSACK GPIOA
+#define BUSACK 12
+//#define P_HALT GPIOA
+//#define HALT 12
+#define P_IOCS1 GPIOA
+#define IOCS1 11
+#define P_NMI GPIOB
+#define NMI 7
+#define P_RST GPIOB
+#define RST 6
+
+#define ADp1_OFS 0
+#define ADp1_WIDTH 8
+#define ADp1_SHIFT 1
+#define ADp1_PORT GPIOA
+
+#define ADp2_OFS ADp1_WIDTH
+#define ADp2_WIDTH 8
+#define ADp2_SHIFT 0
+#define ADp2_PORT GPIOC
+
+#define ADp3_OFS (ADp2_OFS+ADp2_WIDTH)
+#define ADp3_WIDTH 3
+#define ADp3_SHIFT 10
+#define ADp3_PORT GPIOC
+
+#define ADunbuff1_WIDTH 1
+#define ADunbuff1_SHIFT 8
+#define ADunbuff1_PORT GPIOA
+
+#define ADunbuff2_WIDTH 2
+#define ADunbuff2_SHIFT 6
+#define ADunbuff2_PORT GPIOC
+
+#define ADunbuff3_WIDTH 3
+#define ADunbuff3_SHIFT 10
+#define ADunbuff3_PORT GPIOC
+
+#define DB_OFS 0
+#define DB_WIDTH 8
+#define DB_SHIFT 8
+#define DB_PORT GPIOB
+
+#define GPIO_ME GPIO_(ME)
+#define GPIO_RD GPIO_(RD)
+#define GPIO_WR GPIO_(WR)
+#define GPIO_BUSREQ GPIO_(BUSREQ)
+#define GPIO_BUSACK GPIO_(BUSACK)
+//#define GPIO_HALT GPIO_(HALT)
+#define GPIO_IOCS1 GPIO_(IOCS1)
+#define GPIO_NMI GPIO_(NMI)
+#define GPIO_RST GPIO_(RST)
+
+#define Z80_O_ME BBIO_PERIPH(P_ME+ODR, ME)
+#define Z80_O_RD BBIO_PERIPH(P_RD+ODR, RD)
+#define Z80_O_WR BBIO_PERIPH(P_WR+ODR, WR)
+#define Z80_O_BUSREQ BBIO_PERIPH(P_BUSREQ+ODR, BUSREQ)
+#define Z80_O_NMI BBIO_PERIPH(P_NMI+ODR, NMI)
+#define Z80_O_RST BBIO_PERIPH(P_RST+ODR, RST)
+
+#define Z80_I_BUSACK BBIO_PERIPH(P_BUSACK+IDR, BUSACK)
+//#define Z80_I_HALT BBIO_PERIPH(P_HALT+IDR, HALT)
+
+
+#define MASK(n) ((1<<n)-1)
+
+#define IOFIELD_SET(src, ofs, width, shift) \
+ ((((src>>ofs) & MASK(width)) << shift) | ((((~src>>ofs) & MASK(width)) << shift) << 16))
+
+#define IOFIELD_GET(src, width, shift) \
+ ((src>>shift) & MASK(width))
+
+#define CNF_MODE_I_F (GPIO_CNF_INPUT_FLOAT<<2 |GPIO_MODE_INPUT)
+#define CNF_MODE_O_PP (GPIO_CNF_OUTPUT_PUSHPULL<<2 | GPIO_MODE_OUTPUT_10_MHZ)
+
+#define DB_MODE_INPUT ( (CNF_MODE_I_F << (4 * 0)) \
+ | (CNF_MODE_I_F << (4 * 1)) \
+ | (CNF_MODE_I_F << (4 * 2)) \
+ | (CNF_MODE_I_F << (4 * 3)) \
+ | (CNF_MODE_I_F << (4 * 4)) \
+ | (CNF_MODE_I_F << (4 * 5)) \
+ | (CNF_MODE_I_F << (4 * 6)) \
+ | (CNF_MODE_I_F << (4 * 7)))
+
+#define DB_MODE_OUTPUT ( (CNF_MODE_O_PP << (4 * 0)) \
+ | (CNF_MODE_O_PP << (4 * 1)) \
+ | (CNF_MODE_O_PP << (4 * 2)) \
+ | (CNF_MODE_O_PP << (4 * 3)) \
+ | (CNF_MODE_O_PP << (4 * 4)) \
+ | (CNF_MODE_O_PP << (4 * 5)) \
+ | (CNF_MODE_O_PP << (4 * 6)) \
+ | (CNF_MODE_O_PP << (4 * 7)))
+
+
+/*--------------------------------------------------------------------------*/
+
+static void tim16_setup(void)
+{
+ RCC_APB2RSTR |= RCC_APB2RSTR_TIM16RST;
+ RCC_APB2RSTR &= ~RCC_APB2RSTR_TIM16RST;
+
+ TIM16_BDTR = TIM_BDTR_MOE;
+
+ TIM16_CCMR1 = 0
+ | TIM_CCMR1_OC1M_FORCE_LOW
+ | TIM_CCMR1_CC1S_OUT;
+
+ TIM16_CCER = TIM_CCER_CC1NE
+ | TIM_CCER_CC1NP;
+
+ TIM16_ARR = 48; /* default */
+ TIM16_CCR1 = 1; /* */
+}
+
+/*--------------------------------------------------------------------------*/
+
+static void tim16_set(int mode)
+{
+ uint16_t cc_mode;
+
+ cc_mode = TIM_CCMR1_CC1S_OUT;
+
+ TIM16_CR1 = TIM_CR1_OPM;
+
+ if (mode < 0)
+ cc_mode |= TIM_CCMR1_OC1M_FORCE_LOW;
+ else if (mode == 0)
+ cc_mode |= TIM_CCMR1_OC1M_FORCE_HIGH;
+ else {
+ TIM16_ARR = mode;
+ cc_mode |= TIM_CCMR1_OC1M_PWM2;
+ }
+
+ TIM16_CCMR1 = cc_mode;
+
+ if (mode > 0)
+ TIM16_CR1 |= TIM_CR1_CEN;
+}
+
+/*--------------------------------------------------------------------------*/
+
+
+
+/*
+ * A0..A6, A8..A13 are buffered. No need to disable.
+ * A7, A14..A18: set to input.
+ */
+
+static void z80_setup_adrbus_tristate(void)
+{
+#if 0
+ gpio_set_mode(ADunbuff1_PORT, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_FLOAT, MASK(ADunbuff1_WIDTH) << ADunbuff1_SHIFT);
+ gpio_set_mode(ADunbuff2_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
+ (MASK(ADunbuff2_WIDTH) << ADunbuff2_SHIFT) | (MASK(ADunbuff3_WIDTH) << ADunbuff3_SHIFT));
+#else
+ GPIO_CRH(GPIOA) = (GPIO_CRH(GPIOA) & ~(0x0f << (4 * 0)))
+ | (CNF_MODE_I_F << (4 * 0));
+ GPIO_CRL(GPIOC) = (GPIO_CRL(GPIOC) & ~((0x0f << (4 * 6)) | (0x0f << (4 * 7))))
+ | ((CNF_MODE_I_F << (4 * 6)) | (CNF_MODE_I_F << (4 * 7)));
+ GPIO_CRH(GPIOC) = (GPIO_CRH(GPIOC) & ~((0x0f << (4*2)) | (0x0f << (4*3)) | (0x0f << (4*4))))
+ | ((CNF_MODE_I_F << (4*2)) | (CNF_MODE_I_F << (4*3)) | (CNF_MODE_I_F << (4*4)));
+#endif
+}
+
+
+static void z80_setup_adrbus_active(void)
+{
+#if 0
+ gpio_set_mode(ADunbuff1_PORT, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, MASK(ADunbuff1_WIDTH) << ADunbuff1_SHIFT);
+ gpio_set_mode(ADunbuff2_PORT, GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
+ (MASK(ADunbuff2_WIDTH) << ADunbuff2_SHIFT) | (MASK(ADunbuff3_WIDTH) << ADunbuff3_SHIFT));
+#else
+ GPIO_CRH(GPIOA) = (GPIO_CRH(GPIOA) & ~(0x0f << (4 * 0)))
+ | (CNF_MODE_O_PP << (4 * 0));
+ GPIO_CRL(GPIOC) = (GPIO_CRL(GPIOC) & ~((0x0f << (4 * 6)) | (0x0f << (4 * 7))))
+ | ((CNF_MODE_O_PP << (4 * 6)) | (CNF_MODE_O_PP << (4 * 7)));
+ GPIO_CRH(GPIOC) = (GPIO_CRH(GPIOC) & ~((0x0f << (4*2)) | (0x0f << (4*3)) | (0x0f << (4*4))))
+ | ((CNF_MODE_O_PP << (4*2)) | (CNF_MODE_O_PP << (4*3)) | (CNF_MODE_O_PP << (4*4)));
+#endif
+}
+
+
+static void z80_setup_dbus_in(void)
+{
+ GPIO_CRH(DB_PORT) = DB_MODE_INPUT;
+}
+
+static void z80_setup_dbus_out(void)
+{
+ GPIO_CRH(DB_PORT) = DB_MODE_OUTPUT;
+}
+
+
+static void z80_setaddress(uint32_t addr)
+{
+ GPIO_BSRR(ADp1_PORT) = IOFIELD_SET(addr, ADp1_OFS, ADp1_WIDTH, ADp1_SHIFT);
+ GPIO_BSRR(ADp2_PORT) = IOFIELD_SET(addr, ADp2_OFS, ADp2_WIDTH, ADp2_SHIFT);
+ GPIO_BSRR(ADp3_PORT) = IOFIELD_SET(addr, ADp3_OFS, ADp3_WIDTH, ADp3_SHIFT);
+}
+
+void z80_setup_bus(void)
+{
+ tim16_setup();
+
+ gpio_set_mode(P_RST, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_RST);
+ Z80_O_BUSREQ = 1;
+ gpio_set_mode(P_BUSREQ, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO_BUSREQ);
+ Z80_O_NMI = 1;
+ gpio_set_mode(P_NMI, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO_NMI);
+ Z80_O_ME = 1;
+ Z80_O_RD = 1;
+ Z80_O_WR = 1;
+ gpio_set_mode(P_ME, GPIO_MODE_OUTPUT_2_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO_ME);
+ gpio_set_mode(P_RD, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO_RD | GPIO_WR);
+
+ //Z80_O_BUSREQ = 0;
+ //while(Z80_I_BUSACK == 1);
+
+ gpio_set_mode(ADp1_PORT, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, MASK(ADp1_WIDTH) << ADp1_SHIFT);
+ gpio_set_mode(ADp2_PORT, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, MASK(ADp2_WIDTH) << ADp2_SHIFT);
+ gpio_set_mode(ADp3_PORT, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, MASK(ADp3_WIDTH) << ADp3_SHIFT);
+
+ z80_setup_dbus_in();
+}
+
+void z80_request_bus(void)
+{
+ Z80_O_BUSREQ = 0;
+ while(Z80_I_BUSACK == 1);
+ z80_setup_adrbus_active();
+}
+
+void z80_release_bus(void)
+{
+ z80_setup_dbus_in();
+ z80_setup_adrbus_tristate();
+ Z80_O_BUSREQ = 1;
+ while(Z80_I_BUSACK == 0);
+}
+
+void z80_reset(level_t level)
+{
+ int x = level ? -1 : 0;
+
+ tim16_set(x);
+
+// Z80_O_RST = level;
+}
+
+void z80_reset_pulse(void)
+{
+ tim16_set(48);
+}
+
+void z80_busreq(level_t level)
+{
+ Z80_O_BUSREQ = level;
+}
+
+#if 0
+int z80_stat_halt(void)
+{
+ return Z80_I_HALT;
+}
+#endif
+
+void z80_write(uint32_t addr, uint8_t data)
+{
+ z80_setaddress(addr);
+ Z80_O_ME = 0;
+ GPIO_BSRR(DB_PORT) = IOFIELD_SET(data, DB_OFS, DB_WIDTH, DB_SHIFT);
+ z80_setup_dbus_out();
+ Z80_O_WR = 0;
+ Z80_O_WR = 1;
+ Z80_O_ME = 1;
+}
+
+uint8_t z80_read(uint32_t addr)
+{
+ uint8_t data;
+
+ z80_setaddress(addr);
+ Z80_O_ME = 0;
+ z80_setup_dbus_in();
+ Z80_O_RD = 0;
+ Z80_O_RD = 0;
+ data = IOFIELD_GET(GPIO_IDR(DB_PORT),DB_WIDTH, DB_SHIFT);
+ Z80_O_RD = 1;
+ Z80_O_ME = 1;
+
+ return data;
+}
+
+
+void z80_memset(uint32_t addr, uint8_t data, int length)
+{
+ z80_setup_dbus_out();
+ Z80_O_ME = 0;
+ while(length--) {
+ z80_setaddress(addr++);
+ GPIO_BSRR(DB_PORT) = IOFIELD_SET(data, DB_OFS, DB_WIDTH, DB_SHIFT);
+ Z80_O_WR = 0;
+ Z80_O_WR = 1;
+ }
+ Z80_O_ME = 1;
+}
+
+void z80_write_block(uint8_t *src, uint32_t dest, uint32_t length)
+{
+ uint8_t data;
+
+ z80_setup_dbus_out();
+ Z80_O_ME = 0;
+ while(length--) {
+ z80_setaddress(dest++);
+ data = *src++;
+ GPIO_BSRR(DB_PORT) = IOFIELD_SET(data, DB_OFS, DB_WIDTH, DB_SHIFT);
+ Z80_O_WR = 0;
+ Z80_O_WR = 1;
+ }
+ Z80_O_ME = 1;
+}
+
+/*
+ 0179' rx.bs_mask: ds 1 ; (buf_len - 1)
+ 017A' rx.in_idx: ds 1 ;
+ 017B' rx.out_idx: ds 1 ;
+ 017C' rx.buf: ds rx.buf_len ;
+ 018B' rx.buf_end equ $-1 ; last byte (start+len-1)
+
+ 018C' tx.bs_mask: ds 1 ; (buf_len - 1)
+ 018D' tx.in_idx: ds 1 ;
+ 018E' tx.out_idx: ds 1 ;
+ 018F' tx.buf: ds tx.buf_len ;
+ 019E' tx.buf_end equ $-1 ; last byte
+*/
+
+
+typedef struct __attribute__((packed)) {
+ uint8_t mask;
+ uint8_t in_idx;
+ uint8_t out_idx;
+ uint8_t buf[];
+} zfifo_t;
+
+
+
+#define FIFO_BUFSIZE_MASK -3
+#define FIFO_INDEX_IN -2
+#define FIFO_INDEX_OUT -1
+
+
+static struct {
+ uint32_t base;
+ uint8_t idx_out,
+ idx_in,
+ mask;
+ } fifo_dsc[NUM_FIFOS];
+
+
+void z80_memfifo_init(const fifo_t f, uint32_t adr)
+{
+
+DBG_P(2, "z80_memfifo_init: %i, %lx\n", f, adr);
+
+ fifo_dsc[f].base = adr;
+
+ z80_request_bus();
+
+ fifo_dsc[f].mask = z80_read(adr + FIFO_BUFSIZE_MASK);
+ fifo_dsc[f].idx_in = z80_read(adr + FIFO_INDEX_IN);
+ fifo_dsc[f].idx_out = z80_read(adr + FIFO_INDEX_OUT);
+
+ z80_release_bus();
+}
+
+
+int z80_memfifo_is_empty(const fifo_t f)
+{
+ int rc = 1;
+
+ if (fifo_dsc[f].base != 0) {
+
+ uint32_t adr = fifo_dsc[f].base + FIFO_INDEX_IN;
+ uint8_t idx;
+
+ z80_request_bus();
+ idx = z80_read(adr);
+ z80_release_bus();
+ rc = idx == fifo_dsc[f].idx_out;
+ }
+
+ return rc;
+}
+
+int z80_memfifo_is_full(const fifo_t f)
+{
+ int rc = 1;
+
+ if (fifo_dsc[f].base != 0) {
+ z80_request_bus();
+ rc = ((fifo_dsc[f].idx_in + 1) & fifo_dsc[f].mask)
+ == z80_read(fifo_dsc[f].base+FIFO_INDEX_OUT);
+ z80_release_bus();
+ }
+ return rc;
+}
+
+uint8_t z80_memfifo_getc(const fifo_t f)
+{
+ uint8_t rc, idx;
+
+ while (z80_memfifo_is_empty(f))
+ ;
+
+ z80_request_bus();
+ idx = fifo_dsc[f].idx_out;
+ rc = z80_read(fifo_dsc[f].base+idx);
+ fifo_dsc[f].idx_out = ++idx & fifo_dsc[f].mask;
+ z80_write(fifo_dsc[f].base+FIFO_INDEX_OUT, fifo_dsc[f].idx_out);
+ z80_release_bus();
+
+ return rc;
+}
+
+
+void z80_memfifo_putc(fifo_t f, uint8_t val)
+{
+ int idx;
+
+ while (z80_memfifo_is_full(f))
+ ;
+
+ z80_request_bus();
+ idx = fifo_dsc[f].idx_in;
+ z80_write(fifo_dsc[f].base+idx, val);
+ fifo_dsc[f].idx_in = ++idx & fifo_dsc[f].mask;
+ z80_write(fifo_dsc[f].base+FIFO_INDEX_IN, fifo_dsc[f].idx_in);
+ z80_release_bus();
+}
+
+/*--------------------------------------------------------------------------*/
+
+static struct {
+ uint32_t base;
+ //uint8_t idx_out, idx_in;
+ uint16_t count;
+ uint8_t buf[256];
+ } msg_fifo;
+
+/*--------------------------------------------------------------------------*/
+
+#if 0
+
+static void tim1_setup(void)
+{
+ RCC_APB2RSTR |= RCC_APB2RSTR_TIM1RST;
+ RCC_APB2RSTR &= ~RCC_APB2RSTR_TIM1RST;
+
+ TIM1_CR1 = 0;
+
+ TIM1_SMCR = 0
+ /* | TIM_SMCR_ETP */
+ /* | TIM_SMCR_ETF_CK_INT_N_2 */
+ | TIM_SMCR_TS_ETRF
+ | TIM_SMCR_SMS_OFF
+ ;
+
+ TIM1_DIER = TIM_DIER_TDE;
+
+
+ TIM1_CCMR1 = 0
+ | TIM_CCMR1_OC1M_FORCE_LOW
+ | TIM_CCMR1_CC1S_OUT;
+
+ TIM1_SMCR |= TIM_SMCR_SMS_TM;
+}
+
+#endif
+
+/*--------------------------------------------------------------------------*/
+
+static void tim1_ch4_setup(void)
+{
+ /* Reset Timer 1 */
+ RCC_APB2RSTR |= RCC_APB2RSTR_TIM1RST;
+ RCC_APB2RSTR &= ~RCC_APB2RSTR_TIM1RST;
+
+ TIM1_CCMR2 = 0
+ | TIM_CCMR2_CC4S_IN_TI2
+ | TIM_CCMR2_IC4F_OFF
+ | TIM_CCMR2_IC4PSC_OFF;
+
+ TIM1_CCER = 0
+ /* | TIM_CCER_CC4P */
+ | TIM_CCER_CC4E;
+
+ /* Enable DMA for channel 4 */
+ TIM1_DIER = TIM_DIER_CC4DE;
+}
+
+/*--------------------------------------------------------------------------*/
+
+static void dma1_ch4_setup(void)
+{
+ DMA1_CCR4 =
+ DMA_CCR_PL_VERY_HIGH
+ | DMA_CCR_MSIZE_8BIT
+ | DMA_CCR_PSIZE_8BIT
+ | DMA_CCR_MINC
+ | DMA_CCR_CIRC;
+
+ DMA1_CMAR4 = (uint32_t) msg_fifo.buf;
+
+#if (DB_SHIFT == 0) || (DB_SHIFT == 8)
+ DMA1_CPAR4 = DB_PORT + IDR + DB_SHIFT/8;
+#else
+ #error "Databus not byte aligned!"
+#endif
+
+ DMA1_CNDTR4 = NELEMS(msg_fifo.buf);
+// msg_fifo.count = NELEMS(msg_fifo.buf);
+ msg_fifo.count = 0;
+ msg_fifo.base = 0;
+
+ DMA1_CCR4 |= DMA_CCR_EN;
+}
+
+/*--------------------------------------------------------------------------*/
+
+void z80_setup_msg_fifo(void)
+{
+ gpio_set_mode(P_BUSACK, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_FLOAT, GPIO_BUSACK | GPIO_IOCS1);
+
+ tim1_ch4_setup();
+ dma1_ch4_setup();
+}
+
+
+void z80_init_msg_fifo(uint32_t addr)
+{
+
+DBG_P(1, "z80_init_msg_fifo: %lx\n", addr);
+
+ z80_request_bus();
+ z80_write(addr+FIFO_INDEX_OUT, z80_read(addr+FIFO_INDEX_IN));
+ z80_release_bus();
+ msg_fifo.base = addr;
+}
+
+
+int z80_msg_fifo_getc(void)
+{
+ int c = -1;
+
+ if (msg_fifo.count != (NELEMS(msg_fifo.buf) - DMA1_CNDTR4)) {
+ c = msg_fifo.buf[msg_fifo.count];
+ if (++msg_fifo.count == NELEMS(msg_fifo.buf))
+ msg_fifo.count = 0;
+
+ if (msg_fifo.base != 0) {
+ z80_request_bus();
+ z80_write(msg_fifo.base+FIFO_INDEX_OUT, msg_fifo.count);
+ z80_release_bus();
+ }
+ }
+
+ return c;
+}
diff --git a/stm32/z80-if.h b/stm32/z80-if.h
new file mode 100644
index 0000000..d7c030d
--- /dev/null
+++ b/stm32/z80-if.h
@@ -0,0 +1,32 @@
+
+typedef enum {LOW, HIGH} level_t;
+
+//static void z80_setup_adrbus_tristate(void);
+//static void z80_setup_adrbus_active(void);
+//static void z80_setup_dbus_in(void);
+//static void z80_setup_dbus_out(void);
+//static void z80_setaddress(uint32_t addr);
+void z80_setup_bus(void);
+void z80_write(uint32_t addr, uint8_t data);
+uint8_t z80_read(uint32_t addr);
+void z80_request_bus(void);
+void z80_release_bus(void);
+void z80_memset(uint32_t addr, uint8_t data, int length);
+void z80_reset(level_t level);
+void z80_reset_pulse(void);
+void z80_busreq(level_t level);
+void z80_write_block(uint8_t *src, uint32_t dest, uint32_t length);
+int z80_stat_halt(void);
+
+
+typedef enum fifo_t {fifo_in, fifo_out, NUM_FIFOS} fifo_t;
+
+void z80_memfifo_init(const fifo_t f, uint32_t adr);
+int z80_memfifo_is_empty(const fifo_t f);
+int z80_memfifo_is_full(const fifo_t f);
+uint8_t z80_memfifo_getc(const fifo_t f);
+void z80_memfifo_putc(fifo_t f, uint8_t val);
+
+void z80_setup_msg_fifo(void);
+void z80_init_msg_fifo(uint32_t addr);
+int z80_msg_fifo_getc(void);
diff --git a/z180/Makefile b/z180/Makefile
new file mode 100644
index 0000000..f676826
--- /dev/null
+++ b/z180/Makefile
@@ -0,0 +1,126 @@
+
+
+SRC := r3init.180 ddtz.180
+SRC += fifoio.180 msgbuf.180 ser1-i.180 console.180
+SRC += romend.180
+
+INC := config.inc z180reg.inc z180.lib
+
+OBJ := $(SRC:.180=.rel)
+
+#CP/M emulator
+CPMEMU = zxcc
+
+#Location of CP/M binaries
+CPMBIN = /usr/local/lib/cpm/bin80
+
+#AS = $(CPMEMU) $(CPMBIN)/m80.com
+AS = $(CPMEMU) slr180.com
+LN = $(CPMEMU) slrnk+.com
+#LN = $(CPMEMU) ccpline.com
+
+AS_OPT := MFS
+
+AS_QUIET = 1
+LN_QUIET = 1
+
+#LNKCMD =
+LN_VERB = /V
+LN_PROG = 0
+LN_DATA = C000
+
+
+.suffixes:
+#.suffixes: .180 .rel
+
+.phony: all
+all: hdrom.c hdrom.h
+
+$(OBJ): $(INC)
+
+hdrom.h: hdrom.c
+
+comma:= ,
+empty:=
+space:= $(empty) $(empty)
+
+ccpline = $(CPMEMU) $(1) -$(subst $(space),$(comma),$(strip $(2)))
+
+define cpm-asm =
+COMMAND="$(AS) -$(basename $<)/$(AS_OPT)"; \
+OUTPUT=$$(mktemp); echo $${COMMAND}; \
+$${COMMAND} > $${OUTPUT}; \
+grep -q '^ 0 Error(s) Detected' $${OUTPUT}; ERROR=$$? ; \
+if [ "$${ERROR}" != "0" ]; then cat $${OUTPUT}; rm -f $@; fi ; \
+exit $${ERROR}
+endef
+
+define cpm-link =
+ COMMAND="$(call ccpline, slrnk+, $(basename $@)/H/M /V \
+ /P:$(LN_PROG) /D:$(LN_DATA) $(basename $^) /E /Q)";\
+ OUTPUT=$$(mktemp); echo $${COMMAND};\
+ $${COMMAND} > $${OUTPUT};\
+ ERROR=0;\
+ cat $${OUTPUT};\
+ grep -q ' Duplicate Symbol ' $${OUTPUT} && ERROR=2; \
+ grep -q '\- Previously Defined' $${OUTPUT} && ERROR=2; \
+ [ "$${ERROR}" = "0" ] && grep -q '^ ** ' $${OUTPUT} && ERROR=1 ; \
+ [ "$${ERROR}" != "0" ] && rm -f $@; \
+ exit $${ERROR}
+endef
+
+#Use: MAKESYM Filename[.ext][/PXXXX][/DXXXX][/CXXXX]
+#egrep '^[[:xdigit:]]{4}[[:space:]]+[[:xdigit:]]{4}[[:space:]]+D.*r3init\.rel' hdrom.map
+define cpm-mksym =
+COMMAND="$(CPMEMU) makesym -$^ -/P -D"; \
+OUTPUT=$$(mktemp); echo $${COMMAND}; \
+$${COMMAND} > $${OUTPUT}; \
+grep -q '^ 0 Error(s) Detected' $${OUTPUT}; ERROR=$$? ; \
+if [ "$${ERROR}" != "0" ]; then cat $${OUTPUT}; rm -f $@; fi ; \
+exit $${ERROR}
+endef
+
+hdrom.c: hdrom.hex
+ srec_cat -o $@ -c_array $(basename $<) -C_COMpressed -include $< -Intel
+
+hdrom.hex : $(OBJ)
+ @#$(cpm-link)
+ ld80 -o $@ -ms $(@:.hex=.map) -P $(LN_PROG) -D $(LN_DATA) $^
+
+%.rel %lst: %.180
+ @$(cpm-asm)
+
+hdrom.map: hdrom.hex
+
+%.sym: hdrom.map %.lst
+ @$(cpm-mksym)
+
+.phony: clean realclean
+clean:
+ rm -f $(OBJ) $(OBJ:.rel=.lst) $(OBJ:.rel=.sym) hdrom.hex
+
+realclean: clean
+ rm -f *.prn *~ hdrom.map
+
+
+#==================================================================
+
+%.REL: %.MAC
+ @COMMAND="$(AS) =$<"; \
+ OUTPUT=$$(mktemp); echo $${COMMAND}; \
+ $${COMMAND} > $${OUTPUT}; \
+ grep -q 'No Fatal error(s).$$' $${OUTPUT}; ERROR=$$? ; \
+ if [ "$${ERROR}" != "0" ]; then cat $${OUTPUT}; rm $@; fi ; \
+ rm $${OUTPUT}; \
+ exit $${ERROR}
+
+
+%.PRN: %.MAC
+ @COMMAND="$(AS) ,$@=$<"; \
+ OUTPUT=$$(mktemp); echo $${COMMAND}; \
+ $${COMMAND} > $${OUTPUT}; \
+ grep -q 'No Fatal error(s).$$' $${OUTPUT}; ERROR=$$? ; \
+ if [ "$${ERROR}" != "0" ]; then cat $${OUTPUT}; fi ; \
+ rm $${OUTPUT}; \
+ exit $${ERROR}
+
diff --git a/z180/Tupfile b/z180/Tupfile
new file mode 100644
index 0000000..ebdb1d1
--- /dev/null
+++ b/z180/Tupfile
@@ -0,0 +1,52 @@
+include_rules
+
+PROG = hdrom
+
+SRC = r3init.180
+SRC += ddtz.180
+#SRC += fifoio.180 msgbuf.180 ser1-i.180 console.180
+SRC += ser1-i.180 console.180
+SRC += romend.180
+
+
+AS_OPT = MFS
+
+LN_PROG = 0
+LN_DATA = C000
+
+
+###############################################################################
+# Executables
+
+CPMEMU = zxcc
+
+#AS = $(CPMEMU) ccpline.com
+AS = $(CPMEMU) slr180.com
+
+
+###############################################################################
+
+!AS-plain = |> $(AS) -%B/$(AS_OPT) |> %B.rel | %B.lst
+
+!AS = |> ^ $(AS) -%B/$(AS_OPT)^ set +e; OUTPUT=\$(mktemp);\
+$(AS) -%B/$(AS_OPT) > ${OUTPUT};\
+grep -q '^ 0 Error(s) Detected' ${OUTPUT}; ERROR=$?;\
+[ "${ERROR}" != "0" ] && cat ${OUTPUT};\
+[ "${ERROR}" != "0" ] && rm -f %B.rel;\
+rm -f ${OUTPUT}; exit ${ERROR} \
+|> %B.rel | %B.lst
+
+!LINK = |> ld80 -o %o -ms %O.map -P $(LN_PROG) -D $(LN_DATA) %f |> | %O.map
+
+#ifndef DEBUG
+
+: foreach $(SRC) |> !AS |> {objs}
+: {objs} |> !LINK |> $(PROG).hex
+: $(PROG).hex |> srec_cat -o %o -c_array %B -C_COMpressed -include %f -Intel |> $(PROG).c | $(PROG).h
+
+
+#COMMAND="$(AS) -%B/$(AS_OPT)"; \
+
+
+#endif
+
diff --git a/z180/config.inc b/z180/config.inc
new file mode 100644
index 0000000..b03761c
--- /dev/null
+++ b/z180/config.inc
@@ -0,0 +1,105 @@
+
+
+FOSC equ 18432 ;Oscillator frequency [KHz]
+PHI equ FOSC/2 ;CPU frequency
+
+;-----------------------------------------------------
+; Programmable Reload Timer (PRT)
+
+PRT_PRE equ 20 ;PRT prescaler
+
+; Reload value for 10 ms Int. (0.1KHz):
+; tc10ms = phi/prescale/0.1KHz = phi / (prescale/10)
+
+PRT_TC10MS equ PHI / (PRT_PRE/10)
+
+;-----------------------------------------------------
+; MMU
+
+SYS$CBAR equ 0C8h
+USR$CBAR equ 0F0h
+
+
+BANKS equ 18 ;max nr. of banks
+
+;-----------------------------------------------------
+
+CREFSH equ 0 ;Refresh rate register (disable refresh)
+CWAITIO equ 3 shl IWI0 ;Max I/O Wait States, 0 Memory Wait States
+
+
+ROMSYS equ 0
+
+ if ROMSYS
+c$rom equ 0a5h
+ROM_EN equ 0C0h
+ROM_DIS equ ROMEN+1
+CWAITROM equ 2 shl MWI0
+ endif
+
+
+DRSTNUM equ 30h ;DDTZ Restart vector (breakpoints)
+
+
+msg_fb_len equ 256
+rx.buf_len equ 20h
+tx.buf_len equ 80h
+rx.buf_len equ 20h
+tx.buf_len equ 80h
+
+
+s1.rx_len equ 256 ;Serial 1 (ASCI1) buffers
+s1.tx_len equ 256 ;
+
+PMSG equ 80h
+
+;-----------------------------------------------------
+; Definition of (locical) top 2 memory pages
+
+sysram_start equ 0FE00h
+stacksize equ 80
+
+isvsw_loc equ 0FEE0h
+
+ivtab equ 0ffc0h ;int vector table
+iv2tab equ ivtab + 2*9
+
+
+
+;-----------------------------------------------------
+
+
+o.mask equ -3
+o.in_idx equ -2
+o.out_idx equ -1
+
+ .lall
+
+mkbuf macro name,size
+ if ((size & (size-1)) ne 0) or (size gt 256)
+ .printx Error: buffer ^size must be power of 2 and in range 0..256!
+ name&.mask equ ;wrong size error
+ else
+ ds 3
+ name:: ds size
+ name&.mask equ low (size-1)
+ if size ne 0
+ name&.end equ $-1
+ name&.len equ size
+ endif
+ endif
+endm
+
+;-----------------------------------------------------
+
+inidat macro
+ cseg
+??ps.a defl $
+ endm
+
+inidate macro
+??ps.len defl $ - ??ps.a
+ dseg
+ ds ??ps.len
+ endm
+
diff --git a/z180/console.180 b/z180/console.180
new file mode 100644
index 0000000..d7b5ef6
--- /dev/null
+++ b/z180/console.180
@@ -0,0 +1,48 @@
+ ;page 255
+ .z80
+
+
+ global $coninit
+ global $cists,$ci
+ global $co
+
+
+ extrn ser.init,ser.instat,ser.in,ser.out
+; extrn f.init,f.in,f.out,f.i.st
+; extrn msg.co
+
+
+ include config.inc
+ include z180reg.inc
+
+ cseg
+;
+;
+
+$coninit:
+; call f.init
+ call ser.init
+ ret
+
+$cists:
+; call f.i.st
+; ret nz
+ call ser.instat
+ ret
+
+$ci:
+; call f.i.st
+; jp nz,f.in
+ call ser.instat
+ jp nz,ser.in
+ jr $ci
+
+;$costs:
+; jp f.o.st
+
+$co:
+; call msg.co
+ jp ser.out
+
+ end
+
diff --git a/z180/ddtz.180 b/z180/ddtz.180
new file mode 100644
index 0000000..77da910
--- /dev/null
+++ b/z180/ddtz.180
@@ -0,0 +1,6370 @@
+ page 255
+ .z80
+
+ external $ci, $co, $cists
+
+ global ddtz,bpent
+ global $stack
+
+
+ include config.inc
+ include z180reg.inc
+ include z180.lib
+
+BS equ 08h
+TAB equ 09h
+CR equ 0dh
+LF equ 0ah
+DEL equ 7fh
+CNTRX equ 'X'-'@'
+
+TPA equ 100h
+TOPRAM equ 0f000h
+
+
+MEMDUMP_CNT equ 16 ;mem dump bytes per line
+BP_CNT equ 12 ;number of breakbpoints
+
+
+;--------------------------------------------------
+;
+
+; copy code to common memory and execute it there
+comst macro
+ call ?excom
+ ds 1
+?lcs defl $
+ endm
+
+; mark end of common code snippet
+comend macro
+?lce defl $
+?lclen defl ?lce-?lcs
+ org ?lcs-1
+ db ?lclen
+ org ?lce
+ ifndef ?lcmax
+?lcmax defl 0
+ endif
+ if ?lclen gt ?lcmax
+?lcmax defl ?lclen
+ endif
+ endm
+
+; repeat execution of last common code snippet
+comrep macro
+ call ?exclst
+ endm
+
+
+
+ cseg
+
+;----------------------------------------------------------
+
+MSG:
+ DB 'DDT/Z - HD64180 (ROM)'
+ DB CR,LF,0
+
+HLPMSG:
+ DB 'DDT/Z180 (ROM) Commands:',CR,LF
+ DB '> @ examine/substitute the displacement register @',CR,LF
+ DB '> A [address] Assemble',CR,LF
+ DB '> B[X] display [or clear] all Breakpoints',CR,LF
+ DB ' B breakp [:count] [breakp..] set Breakpoints',CR,LF
+ DB ' BX address [address..] clear Breakpoints',CR,LF
+ DB '>>C[N][J] [count] trace over Calls [No list] [Jumps only]',CR,LF
+ DB ' C[N][J] W|U expression trace over Calls While|Until ...',CR,LF
+ DB '>>D [startadr] [endadr] Display memory in hex and ascii',CR,LF
+ DB '> G [startadr] [;breakp..] Go [to start] [temporary breakpoints]',CR,LF
+ DB '> H [expression [expression]] compute expressions / show High/max load adr.',CR,LF
+ DB '>>I [port] Input a byte from port',CR,LF
+ DB '>>L [startadr] [endadr] List disassembled code',CR,LF
+ DB '> M[V] startadr endadr destadr Move memory [and verify]',CR,LF
+ DB '>>O [byte] [port] Output a byte to port',CR,LF
+ DB '> Q[J] startadr endadr bytes Qery memory for byte string [Justified]',CR,LF
+ DB '> R [displacement] Read intel hex from console [add displacemt]',CR,LF
+ DB '> S address Substitute memory',CR,LF
+ DB '>>T[N][J] [count] Trace [No list] [Jumps only] [count steps]',CR,LF
+ DB ' T[N][J] W|U expression Trace While|Until expression',CR,LF
+ DB '> V startadr endadr destadr Verify (compare) two memory areas',CR,LF
+ DB '> X[register] eXamine [and substitute] registers',CR,LF
+ DB '> Y[0..9] eXamine [and substitute] Y variables',CR,LF
+ DB '> Z startadr endadr bytes Zap (fill) memory with a byte string',CR,LF
+ DB 0
+
+ddtz:
+ ld sp,$stack
+ ld a,(wstrtflg) ;check warm start flag
+ or a
+ jr nz,ddtz_w
+
+ exx
+ ld hl,sysramc
+ ld de,topcodbeg
+ ld bc,topcodend-topcodbeg
+ ldir
+
+ ld hl,vartab
+ ld de,ddtram
+ ld bc,vartabe-vartab
+ ldir
+ exx
+
+ ld a,e
+ ld (ubbr),a
+
+ddtz_w:
+ ld hl,MSG ;073c
+ call PSTR ;073f
+ call ddtei ;0742
+
+; DDTZ main loop
+
+DDTZML:
+ ld sp,$stack ;0761
+ ld hl,l07eah ;0764
+ ld (CMD_ERR),hl ;0767
+ ld hl,(REG.PC) ;076a
+ ld (OFFS.pc),hl ;076d
+ call sub_0e68h ;0770
+ ld hl,(CMD_RPT) ;0773
+ ld de,DDTZML ;0776
+ call CP.HL.DE ;0779
+ ld a,'>' ;077c
+ call OUTCHAR ;077e
+ call nz,OUTCHAR ;0781
+ call z,OUTBL ;0784
+ call INLINE ;0787
+ call SKIPBL ;078a
+ jr z,exe_hl ;078d
+ ld hl,DDTZML ;078f
+ ld (CMD_RPT),hl ;0792
+ inc de ;0795
+ sub '?' ;0796
+ jr c,ERROR ;0798
+ cp 'Z'+1-'?' ;079a
+ jr nc,ERROR ;079c
+ add a,a ;079e
+ ld hl,CMDTAB ;079f
+ call ADD_HL_A ;07a2
+ ld a,(hl) ;07a5
+ inc hl ;07a6
+ ld h,(hl) ;07a7
+ ld l,a ;07a8
+ jr exe_hl ;07a9
+ERROR:
+ ld hl,(CMD_ERR) ;07ab
+exe_hl:
+ call CALL.HL ;07ae
+ jr DDTZML ;07b1
+
+CALL.HL:
+ jp (hl) ;07b3
+
+
+CMDTAB:
+ defw CMD.?
+ defw CMD.@ ;07b4
+ defw CMD.A ;07b6
+ defw CMD.B ;07b8
+ defw CMD.C ;07ba
+ defw CMD.D ;07bc
+ defw ERROR ;07be
+ defw ERROR ;07c0
+ defw CMD.G ;07c2
+ defw CMD.H ;07c4
+ defw CMD.I ;07c6
+ defw ERROR ;07c8
+ defw ERROR ;07ca
+ defw CMD.L ;07cc
+ defw CMD.M ;07ce
+ defw ERROR ;07d0
+ defw CMD.O ;07d2
+ defw ERROR ;07d4
+ defw CMD.Q ;07d6
+ defw CMD.R ;07d8
+ defw CMD.S ;07da
+ defw CMD.T ;07dc
+ defw ERROR ;07de
+ defw CMD.V ;07e0
+ defw ERROR ;07e2
+ defw CMD.X ;07e4
+ defw CMD.Y ;07e6
+ defw CMD.Z ;07e8
+l07eah:
+ ld a,'?' ;07ea
+ call OUTCHAR ;07ec
+ jp CRLF ;07ef
+
+CMD.?:
+ call assert_eol
+ ld hl,HLPMSG
+ call PSTR
+ ret
+
+DELC:
+ ld a,b ;07f2
+ or a ;07f3
+ ret z ;07f4
+ ld a,BS ;07f5
+ call $co ;07f7
+ ld a,' ' ;07fa
+ call $co ;07fc
+ ld a,BS ;07ff
+ call $co ;0801
+ dec hl ;0804
+ dec b ;0805
+ inc c ;0806
+ ld a,(hl) ;0807
+ cp ' ' ;0808
+ ret nc ;080a
+ ld a,BS ;080b
+ call $co ;080d
+ ld a,' ' ;0810
+ call $co ;0812
+ ld a,BS ;0815
+ call $co ;0817
+ ret ;081a
+DELL:
+ ld a,b ;081b
+ or a ;081c
+ ret z ;081d
+ call DELC ;081e
+ jr DELL ;0821
+
+INLINE:
+ push hl ;0823
+ ld hl,CI.BUF ;0824
+ ld c,(hl) ;0827
+ inc hl ;0828
+ ld b,000h ;0829
+ inc hl ;082b
+inlnxtch:
+ ld a,c ;082c
+ or a ;082d
+ jr z,inl_e ;082e
+ call $ci ;0830
+ cp CR ;0833
+ jr z,inl_e ;0835
+ cp LF ;0837
+ jr z,inl_e ;0839
+ cp BS ;083b
+ jr nz,l0844h ;083d
+ call DELC ;083f
+ jr inlnxtch ;0842
+l0844h:
+ cp DEL ;0844
+ jr nz,l084dh ;0846
+ call DELC ;0848
+ jr inlnxtch ;084b
+l084dh:
+ cp CNTRX ;084d
+ jr nz,l0856h ;084f
+ call DELL ;0851
+ jr inlnxtch ;0854
+l0856h:
+ cp TAB ;0856
+ jr nz,l085ch ;0858
+ ld a,' ' ;085a
+l085ch:
+ ld (hl),a ;085c
+ cp ' ' ;085d
+ jr nc,l0869h ;085f
+ ld a,'^' ;0861
+ call $co ;0863
+ ld a,(hl) ;0866
+ add a,'@' ;0867
+l0869h:
+ call $co ;0869
+ inc hl ;086c
+ inc b ;086d
+ dec c ;086e
+ jr inlnxtch ;086f
+
+inl_e:
+ ld hl,ci.buf+1 ;0871
+ ld (hl),b ;0874
+ call CRLF ;0875
+ ld de,ci.buf+1 ;0878
+ ld a,(de) ;087b
+ ld b,a ;087c
+ ld c,000h ;087d
+ inc b ;087f
+l0880h:
+ inc de ;0880
+ dec b ;0881
+ jr z,l08b2h ;0882
+ ld a,(de) ;0884
+ bit 0,c ;0885
+ call z,UPCASE ;0887
+ ld (de),a ;088a
+ cp '''' ;088b
+ jr nz,l0880h ;088d
+ push de ;088f
+ dec de ;0890
+ ld a,(de) ;0891
+ cp '''' ;0892
+ jr z,l08aeh ;0894
+ dec de ;0896
+ ld a,(de) ;0897
+ cp '^' ;0898
+ jr z,l08a2h ;089a
+ dec de ;089c
+ ld a,(de) ;089d
+ cp '^' ;089e
+ jr nz,l08aeh ;08a0
+l08a2h:
+ inc de ;08a2
+ push bc ;08a3
+ call sub_0a0eh ;08a4
+ pop bc ;08a7
+ dec de ;08a8
+ ld a,(de) ;08a9
+ cp '''' ;08aa
+ jr z,l08afh ;08ac
+l08aeh:
+ inc c ;08ae
+l08afh:
+ pop de ;08af
+ jr l0880h ;08b0
+l08b2h:
+ xor a ;08b2
+ ld (de),a ;08b3
+ ld de,ci.buf+2 ;08b4
+ pop hl ;08b7
+ ret ;08b8
+
+UPCASE:
+ cp 'a' ;08b9
+ ret c ;08bb
+ cp 'z'+1 ;08bc
+ ret nc ;08be
+ and 05fh ;08bf
+ ret ;08c1
+
+out.hl.@:
+ call out.hl ;08c2
+ push de ;08c5
+ push hl ;08c6
+ ld de,(offs.@) ;08c7
+ ld a,d ;08cb
+ or e ;08cc
+ jr z,l08ddh ;08cd
+ call OUTBL ;08cf
+ ld a,'@' ;08d2
+ call OUTCHAR ;08d4
+ and a ;08d7
+ sbc hl,de ;08d8
+ call out.hl ;08da
+l08ddh:
+ pop hl ;08dd
+ pop de ;08de
+ ret ;08df
+
+out.bin.w:
+ ld a,h ;08e0
+ call out.bin.b ;08e1
+ ld a,l ;08e4
+out.bin.b:
+ ld b,008h ;08e5
+l08e7h:
+ add a,a ;08e7
+ push af ;08e8
+ ld a,0 ;08e9
+ adc a,a ;08eb
+ call out.digit ;08ec
+ pop af ;08ef
+ djnz l08e7h ;08f0
+ ld a,'"' ;08f2
+ jp OUTCHAR ;08f4
+
+sub_08f7h:
+ ld a,'-' ;08f7
+ call OUTCHAR ;08f9
+ dec hl ;08fc
+ jp cpl.hl ;08fd
+
+out.hl.decm:
+ push hl ;0900
+ call sub_08f7h ;0901
+ db 3eh ;0904 ld a,0E5h
+out.hl.dec:
+ push hl
+ ld b,6 ;0906
+ call sub_0917h ;0908
+ pop hl ;090b
+ ld a,'.' ;090c
+ call OUTCHAR ;090e
+l0911h:
+ call OUTBL ;0911
+ djnz l0911h ;0914
+ ret ;0916
+
+
+sub_0917h:
+ dec b ;0917
+ push de ;0918
+ ld de,10 ;0919
+ call DIV_HL_DE ;091c
+ ld a,h ;091f
+ or l ;0920
+ call nz,sub_0917h ;0921
+ ld a,e ;0924
+ pop de ;0925
+ jr out.digit ;0926
+
+sub_0928h:
+ push hl ;0928
+ call sub_08f7h ;0929
+ call out.hl ;092c
+ pop hl ;092f
+ ret ;0930
+out.hl:
+ ld a,h ;0931
+ call out.hex ;0932
+ ld a,l ;0935
+out.hex:
+ push af ;0936
+ rra ;0937
+ rra ;0938
+ rra ;0939
+ rra ;093a
+ call out.digit ;093b
+ pop af ;093e
+out.digit:
+ and 00fh ;093f
+ cp 10 ;0941
+ jr c,l0947h ;0943
+ add a,007h ;0945
+l0947h:
+ add a,'0' ;0947
+ jr OUTCHAR ;0949
+l094bh:
+ ld a,'-' ;094b
+ call OUTCHAR ;094d
+ ld a,040h ;0950
+out.ascii:
+ ex af,af' ;0952
+ call outquote ;0953
+ ex af,af' ;0956
+ push af ;0957
+ res 7,a ;0958
+ cp ' ' ;095a
+ jr nc,l0960h ;095c
+ sub 0c0h ;095e
+l0960h:
+ call OUTCHAR ;0960
+ push af ;0963
+ cp '''' ;0964
+ call z,OUTCHAR ;0966
+ pop af ;0969
+ ex af,af' ;096a
+ call outquote ;096b
+ pop af ;096e
+ or a ;096f
+ ld a,'.' ;0970
+ call m,OUTCHAR ;0972
+ ex af,af' ;0975
+ jr c,l094bh ;0976
+ ret ;0978
+
+outquote:
+ ld a,'''' ;0979
+OUTCHAR:
+ push hl ;097b
+ push af ;097c
+ and 07fh ;097d
+ call $co ;097f
+ ld hl,CON.COL ;0982
+ inc (hl) ;0985
+ pop af ;0986
+ pop hl ;0987
+ ret ;0988
+
+inchar:
+ call $cists ;0989
+ and a ;098c
+ ret z ;098d
+ call $ci ;098e
+ scf ;0991
+ ret ;0992
+
+PSTR:
+ ld c,000h ;0993
+l0995h:
+ ld a,(hl) ;0995
+ and a ;0996
+ ret z ;0997
+ call OUTCHAR ;0998
+ inc c ;099b
+ inc hl ;099c
+ and a ;099d
+ ret m ;099e
+ jr l0995h ;099f
+
+outbl6:
+ call outbl2 ;09a1
+outbl4:
+ call outbl2 ;09a4
+outbl2:
+ call OUTBL ;09a7
+OUTBL:
+ ld a,' ' ;09aa
+ jr OUTCHAR ;09ac
+CRLF:
+ call inchar ;09ae
+ ld a,CR ;09b1
+ call OUTCHAR ;09b3
+ ld a,LF ;09b6
+ call OUTCHAR ;09b8
+ ld a,000h ;09bb
+ ld (CON.COL),a ;09bd
+ jp c,DDTZML ;09c0
+ ret ;09c3
+
+ADD_HL_A:
+ add a,l ;09c4
+ ld l,a ;09c5
+ ret nc ;09c6
+ inc h ;09c7
+ ret ;09c8
+
+SKIPBL0:
+ inc de ;09c9
+SKIPBL:
+ ld a,(de) ;09ca
+ cp ' ' ;09cb
+ jr z,SKIPBL0 ;09cd
+ cp 009h ;09cf
+ jr z,SKIPBL0 ;09d1
+ or a ;09d3
+ ret ;09d4
+
+skip_to_nextarg:
+ call SKIPBL ;09d5
+ cp ',' ;09d8
+ ret nz ;09da
+ inc de ;09db
+ call SKIPBL ;09dc
+ cp a ;09df
+ ret ;09e0
+
+assert_eol:
+ call SKIPBL ;09e1
+ ret z ;09e4
+l09e5h:
+ jp ERROR ;09e5
+
+chk.sp:
+ push hl ;09e8
+ push de ;09e9
+ ld hl,0 ;09ea
+ add hl,sp ;09ed
+ ld de,$stack-50 ;09ee
+ call CP.HL.DE ;09f1
+ pop de ;09f4
+ pop hl ;09f5
+ jr c,l09e5h ;09f6
+ ret ;09f8
+
+CP.HL.DE:
+ and a ;09f9
+ sbc hl,de ;09fa
+ add hl,de ;09fc
+ ret ;09fd
+
+lookupch:
+ ld b,000h ;09fe
+l0a00h:
+ ld a,(hl) ;0a00
+ and a ;0a01
+ ret z ;0a02
+ ld a,(de) ;0a03
+ cp (hl) ;0a04
+ jr z,l0a0bh ;0a05
+ inc hl ;0a07
+ inc b ;0a08
+ jr l0a00h ;0a09
+l0a0bh:
+ scf ;0a0b
+ inc de ;0a0c
+ ret ;0a0d
+
+sub_0a0eh:
+ ld hl,b_0x132A_start ;0a0e
+ ld b,07fh ;0a11
+ jr l0a17h ;0a13
+
+sub_0a15h:
+ ld b,0ffh ;0a15
+l0a17h:
+ inc b ;0a17
+ ld a,(hl) ;0a18
+ and a ;0a19
+ ret z ;0a1a
+ call l0a27 ;0a1b
+ jr nc,l0a17h ;0a1e
+ res 7,b ;0a20
+ ret ;0a22
+
+sub_0a23h:
+ push bc ;0a23
+ res 7,b ;0a24
+ db 3eh ;0a26 ld a,0c5h
+l0a27:
+ push bc ;0a27
+ push de ;0a28
+l0a29h:
+ ld a,(de) ;0a29
+ xor (hl) ;0a2a
+ and 07fh ;0a2b
+ jr nz,l0a41h ;0a2d
+ bit 7,(hl) ;0a2f
+ inc hl ;0a31
+ inc de ;0a32
+ jr z,l0a29h ;0a33
+ scf ;0a35
+ bit 7,b ;0a36
+ call z,sub_0d20h ;0a38
+ jr nc,l0a44h ;0a3b
+ pop af ;0a3d
+ scf ;0a3e
+ pop bc ;0a3f
+ ret ;0a40
+
+l0a41h:
+ call sub_0a50h ;0a41
+l0a44h:
+ pop de ;0a44
+ and a ;0a45
+ pop bc ;0a46
+ ret ;0a47
+
+sub_0a48h:
+ inc b ;0a48
+l0a49h:
+ dec b ;0a49
+ ret z ;0a4a
+ call sub_0a50h ;0a4b
+ jr l0a49h ;0a4e
+sub_0a50h:
+ ld a,(hl) ;0a50
+ and a ;0a51
+ ret z ;0a52
+l0a53h:
+ ld a,(hl) ;0a53
+ inc hl ;0a54
+ and a ;0a55
+ ret m ;0a56
+ jr l0a53h ;0a57
+
+get_arg3:
+ call get_arg_range ;0a59
+ push hl ;0a5c
+ push bc ;0a5d
+ call skip_to_nextarg ;0a5e
+ call get_arg ;0a61
+ ex de,hl ;0a64
+ pop bc ;0a65
+ pop hl ;0a66
+ ret ;0a67
+
+sub_0a68h:
+ call EXPR ;0a68
+ jr c,error0 ;0a6b
+ ret ;0a6d
+
+get_arg:
+ call sub_0a68h ;0a6e
+l0a71h:
+ jp assert_eol ;0a71
+
+get_lastarg_def:
+ call get_arg_def ;0a74
+ jr l0a71h ;0a77
+
+get_arg_def:
+ push hl ;0a79
+ call EXPR ;0a7a
+ jr c,l0a80h ;0a7d
+ ex (sp),hl ;0a7f
+l0a80h:
+ pop hl ;0a80
+ ret ;0a81
+
+sub_0a82h:
+ call sub_0a87h ;0a82
+ jr l0a71h ;0a85
+
+sub_0a87h:
+ db 0e6h ;0a87 and 037h (clear carry)
+get_arg_range:
+ scf
+ ex af,af' ;0a89
+ push bc ;0a8a
+ push hl ;0a8b
+ call EXPR ;0a8c
+ jr nc,l0a97h ;0a8f
+ ex af,af' ;0a91
+ jr c,error0 ;0a92
+ ex af,af' ;0a94
+ pop hl ;0a95
+
+ defb 03eh ;0a96
+l0a97h:
+ pop af ;0a97
+ call sub_0aa5h ;0a98
+ jr nc,l0aa3h ;0a9b
+ ex af,af' ;0a9d
+ pop bc ;0a9e
+ ret nc ;0a9f
+error0:
+ jp ERROR ;0aa0
+
+l0aa3h:
+ pop af ;0aa3
+ ret ;0aa4
+
+sub_0aa5h:
+ call skip_to_nextarg ;0aa5
+ cp 'S' ;0aa8
+ jr nz,l0aadh ;0aaa
+ inc de ;0aac
+l0aadh:
+ push hl ;0aad
+ push af ;0aae
+ call EXPR ;0aaf
+ jr c,l0ac3h ;0ab2
+ ld b,h ;0ab4
+ ld c,l ;0ab5
+ pop af ;0ab6
+ pop hl ;0ab7
+ jr z,l0ac1h ;0ab8
+ ld a,c ;0aba
+ sub l ;0abb
+ ld c,a ;0abc
+ ld a,b ;0abd
+ sbc a,h ;0abe
+ ld b,a ;0abf
+ inc bc ;0ac0
+l0ac1h:
+ and a ;0ac1
+ ret ;0ac2
+
+l0ac3h:
+ pop af ;0ac3
+ pop hl ;0ac4
+ jr z,error0 ;0ac5
+ scf ;0ac7
+ ret ;0ac8
+
+EXPR:
+ call SKIPBL ;0ac9
+EXPR1:
+ call do_subexpr ;0acc
+ ret c ;0acf
+ call do_rel_op ;0ad0
+ ret nc ;0ad3
+ push bc ;0ad4
+ push hl ;0ad5
+ call do_subexpr ;0ad6
+ jr c,error0 ;0ad9
+ ex de,hl ;0adb
+ ex (sp),hl ;0adc
+ and a ;0add
+ sbc hl,de ;0ade
+ ld hl,0ffffh ;0ae0
+ pop de ;0ae3
+ ret ;0ae4
+
+do_op_eq:
+ jr z,l0af8h ;0ae5
+ jr l0af7h ;0ae7
+do_op_ne:
+ jr nz,l0af8h ;0ae9
+ jr l0af7h ;0aeb
+do_op_le:
+ jr z,l0af8h ;0aed
+do_op_lt:
+ jr c,l0af8h ;0aef
+ jr l0af7h ;0af1
+do_op_gt:
+ jr z,l0af7h ;0af3
+do_op_ge:
+ jr nc,l0af8h ;0af5
+l0af7h:
+ inc hl ;0af7
+l0af8h:
+ and a ;0af8
+ ret ;0af9
+
+do_rel_op:
+ push hl ;0afa
+ ld hl,tab_eq_le_ge ;0afb
+ call lookupch ;0afe
+ jr nc,l0b28h ;0b01
+ ld a,b ;0b03
+ or a ;0b04
+ jr z,l0b1ch ;0b05
+ ld a,(de) ;0b07
+ cp '=' ;0b08
+ jr nz,l0b11h ;0b0a
+ inc de ;0b0c
+ inc b ;0b0d
+ inc b ;0b0e
+ jr l0b1ch ;0b0f
+
+l0b11h:
+ bit 0,b ;0b11
+ jr z,l0b1ch ;0b13
+ cp '>' ;0b15
+ jr nz,l0b1ch ;0b17
+ inc de ;0b19
+ ld b,005h ;0b1a
+l0b1ch:
+ ld hl,tab_func_eqlege ;0b1c
+ ld a,b ;0b1f
+ add a,a ;0b20
+ call ADD_HL_A ;0b21
+ ld c,(hl) ;0b24
+ inc hl ;0b25
+ ld b,(hl) ;0b26
+ scf ;0b27
+l0b28h:
+ pop hl ;0b28
+ ret ;0b29
+
+tab_eq_le_ge:
+ db '=<>',0
+
+tab_func_eqlege:
+ defw do_op_eq ;0b2e
+ defw do_op_lt ;0b30
+ defw do_op_gt ;0b32
+ defw do_op_le ;0b34
+ defw do_op_ge ;0b36
+ defw do_op_ne ;0b38
+
+do_subexpr:
+ call do_factor ;0b3a
+ ret c ;0b3d
+l0b3eh:
+ call do_binary_op ;0b3e
+ push hl ;0b41
+ push bc ;0b42
+ call do_factor ;0b43
+ pop bc ;0b46
+ ex de,hl ;0b47
+ ex (sp),hl ;0b48
+ jr nc,l0b52h ;0b49
+ pop de ;0b4b
+ ld a,b ;0b4c
+ or c ;0b4d
+ ret z ;0b4e
+ jp ERROR ;0b4f
+
+l0b52h:
+ ld a,b ;0b52
+ or c ;0b53
+ push bc ;0b54
+ ret nz ;0b55
+ pop bc ;0b56
+do_op_add:
+ add hl,de ;0b57
+l0b58h:
+ pop de ;0b58
+ jr l0b3eh ;0b59
+
+do_op_sub:
+ and a ;0b5b
+ sbc hl,de ;0b5c
+ jr l0b58h ;0b5e
+
+do_op_mlt:
+ push bc ;0b60
+ ld b,h
+ ld c,e
+ ld h,e
+ ld e,l
+ mlt bc
+ mlt de
+ mlt hl
+ ld a,h
+ add a,c
+ add a,e
+ ld h,a
+ pop bc ;0b72
+ jr l0b58h ;0b73
+
+do_op_div:
+ call DIV_HL_DE ;0b75
+ jr l0b58h ;0b78
+
+do_op_mod:
+ call DIV_HL_DE ;0b7a
+ ex de,hl ;0b7d
+ jr l0b58h ;0b7e
+
+DIV_HL_DE:
+ push bc ;0b80
+ ex de,hl ;0b81
+ ld b,h ;0b82
+ ld c,l ;0b83
+ ld hl,0 ;0b84
+ ld a,16 ;0b87
+l0b89h:
+ push af ;0b89
+ add hl,hl ;0b8a
+ ex de,hl ;0b8b
+ xor a ;0b8c
+ add hl,hl ;0b8d
+ ex de,hl ;0b8e
+ adc a,l ;0b8f
+ sub c ;0b90
+ ld l,a ;0b91
+ ld a,h ;0b92
+ sbc a,b ;0b93
+ ld h,a ;0b94
+ inc de ;0b95
+ jr nc,l0b9ah ;0b96
+ add hl,bc ;0b98
+ dec de ;0b99
+l0b9ah:
+ pop af ;0b9a
+ dec a ;0b9b
+ jr nz,l0b89h ;0b9c
+ ex de,hl ;0b9e
+ pop bc ;0b9f
+ ret ;0ba0
+
+do_op_and:
+ ld a,h ;0ba1
+ and d ;0ba2
+ ld h,a ;0ba3
+ ld a,l ;0ba4
+ and e ;0ba5
+ ld l,a ;0ba6
+ jr l0b58h ;0ba7
+
+do_op_or:
+ ld a,h ;0ba9
+ or d ;0baa
+ ld h,a ;0bab
+ ld a,l ;0bac
+ or e ;0bad
+ ld l,a ;0bae
+ jr l0b58h ;0baf
+
+do_op_xor:
+ ld a,h ;0bb1
+ xor d ;0bb2
+ ld h,a ;0bb3
+ ld a,l ;0bb4
+ xor e ;0bb5
+ ld l,a ;0bb6
+ jr l0b58h ;0bb7
+
+do_binary_op:
+ push hl ;0bb9
+ ld hl,tab_op_a ;0bba
+ call lookupch ;0bbd
+ ld a,b ;0bc0
+ ld hl,tab_func_opa ;0bc1
+ add a,a ;0bc4
+ call ADD_HL_A ;0bc5
+ ld c,(hl) ;0bc8
+ inc hl ;0bc9
+ ld b,(hl) ;0bca
+ pop hl ;0bcb
+ ret ;0bcc
+
+tab_op_a:
+ DB '+-*/%&!#',0
+tab_func_opa:
+ defw do_op_add ;0bd6
+ defw do_op_sub ;0bd8
+ defw do_op_mlt ;0bda
+ defw do_op_div ;0bdc
+ defw do_op_mod ;0bde
+ defw do_op_and ;0be0
+ defw do_op_or ;0be2
+ defw do_op_xor ;0be4
+ defw 0 ;0be6
+
+fact_factor:
+ call do_factor ;0be8
+ ret nc ;0beb
+ jp ERROR ;0bec
+
+do_factor:
+ call chk.sp ;0bef
+ call get.number ;0bf2
+ ret nc ;0bf5
+ inc de ;0bf6
+ ld hl,TOPRAM ;0bf7
+ cp 'T' ;0bfa
+ ret z ;0bfc
+ ld hl,(HILOD) ;0bfd
+ cp 'H' ;0c00
+ ret z ;0c02
+ ld hl,(MAXLOD) ;0c03
+ cp 'M' ;0c06
+ ret z ;0c08
+ ld hl,TPA ;0c09
+ cp 'L' ;0c0c
+ ret z ;0c0e
+ ld hl,(offs.@) ;0c0f
+ cp '@' ;0c12
+ ret z ;0c14
+ ld hl,(OFFS.pc) ;0c15
+ cp '$' ;0c18
+ ret z ;0c1a
+ cp '-' ;0c1b
+ jr z,fact_factneg ;0c1d
+ cp '~' ;0c1f
+ jr z,fact_factinv ;0c21
+ cp '+' ;0c23
+ jr z,fact_factor ;0c25
+ cp '^' ;0c27
+ jr z,fact_reg.CPU ;0c29
+ cp 'Y' ;0c2b
+ jr z,fact_reg.Y ;0c2d
+ cp '(' ;0c2f
+ jr z,fact_mem ;0c31
+ cp '[' ;0c33
+ jp z,EXPR_BRCKT ;0c35 [ expression ]
+ cp '''' ;0c38
+ jr z,fact_factstring ;0c3a
+ dec de ;0c3c
+ scf ;0c3d
+ ret ;0c3e
+
+fact_reg.Y:
+ call get.decdigit ;0c3f
+ jp c,ERROR ;0c42
+ inc de ;0c45
+ add a,a ;0c46
+ ld hl,reg.Y ;0c47
+ call ADD_HL_A ;0c4a
+ ld a,(hl) ;0c4d
+ inc hl ;0c4e
+ ld h,(hl) ;0c4f
+ ld l,a ;0c50
+ and a ;0c51
+ ret ;0c52
+fact_factstring:
+ ld hl,0 ;0c53
+l0c56h:
+ ld a,(de) ;0c56
+ cp '''' ;0c57
+ jr z,l0c62h ;0c59
+ and a ;0c5b
+ ret z ;0c5c
+l0c5dh:
+ ld h,l ;0c5d
+ ld l,a ;0c5e
+ inc de ;0c5f
+ jr l0c56h ;0c60
+
+l0c62h:
+ inc de ;0c62
+ ld a,(de) ;0c63
+ cp '''' ;0c64
+ jr z,l0c5dh ;0c66
+ sub '.' ;0c68
+ or a ;0c6a
+ ret nz ;0c6b
+ inc de ;0c6c
+ set 7,l ;0c6d
+ ret ;0c6f
+
+fact_reg.CPU:
+ call sub_1315h ;0c70
+ jr nc,l0cbbh ;0c73
+ ld a,(hl) ;0c75
+ inc hl ;0c76
+ ld h,(hl) ;0c77
+ ld l,a ;0c78
+ and a ;0c79
+ bit 0,c ;0c7a
+ ret nz ;0c7c
+ ld h,000h ;0c7d
+ ret ;0c7f
+
+fact_factneg:
+ call fact_factor ;0c80
+ dec hl ;0c83
+cpl.hl:
+ ld a,h ;0c84
+ cpl ;0c85
+ ld h,a ;0c86
+ ld a,l ;0c87
+ cpl ;0c88
+ ld l,a ;0c89
+ ret ;0c8a
+fact_factinv:
+ call fact_factor ;0c8b
+ jr cpl.hl ;0c8e
+
+fact_mem:
+ call EXPR1 ;0c90
+ jr c,l0cbbh ;0c93
+ ld a,(de) ;0c95
+ cp ')' ;0c96
+ jr nz,l0cbbh ;0c98
+ inc de ;0c9a
+ comst
+ ld a,(hl) ;0c9f
+ inc hl ;
+ ld h,(hl) ;
+ comend
+ ld l,a ;0ca7
+ ld a,(de) ;0ca8
+ inc de ;0ca9
+ cp '.' ;0caa
+ ret z ;0cac
+ dec de ;0cad
+ xor a ;0cae
+ ld h,a ;0caf
+ ret ;0cb0
+
+EXPR_BRCKT:
+ call EXPR1 ;0cb1
+ jr c,l0cbbh ;0cb4
+ ld a,(de) ;0cb6
+ cp ']' ;0cb7
+ inc de ;0cb9
+ ret z ;0cba
+l0cbbh:
+ jp ERROR ;0cbb
+
+get.number:
+ call get.hexdigit ;0cbe
+ ret c ;0cc1
+ push de ;0cc2
+l0cc3h:
+ inc de ;0cc3
+ call get.hexdigit ;0cc4
+ jr nc,l0cc3h ;0cc7
+ pop de ;0cc9
+ cp '.' ;0cca
+ jr z,l0d04h ;0ccc
+ cp '"' ;0cce
+ jr z,l0ce9h ;0cd0
+ ld hl,0 ;0cd2
+l0cd5h:
+ call get.hexdigit ;0cd5
+ jr c,l0ce4h ;0cd8
+ add hl,hl ;0cda
+ add hl,hl ;0cdb
+ add hl,hl ;0cdc
+ add hl,hl ;0cdd
+ call ADD_HL_A ;0cde
+ inc de ;0ce1
+ jr l0cd5h ;0ce2
+l0ce4h:
+ xor 'H' ;0ce4
+ ret nz ;0ce6
+ inc de ;0ce7
+ ret ;0ce8
+
+l0ce9h:
+ ld hl,0 ;0ce9
+l0cech:
+ call get.decdigit ;0cec
+l0cefh:
+ inc de ;0cef
+ jr c,l0cf8h ;0cf0
+ add hl,hl ;0cf2
+ call ADD_HL_A ;0cf3
+ jr l0cech ;0cf6
+l0cf8h:
+ cp '"' ;0cf8
+ jp nz,ERROR ;0cfa
+ call get.decdigit ;0cfd
+ jr nc,l0cefh ;0d00
+ or a ;0d02
+ ret ;0d03
+l0d04h:
+ ld hl,0 ;0d04
+l0d07h:
+ call get.decdigit ;0d07
+ inc de ;0d0a
+ jr c,l0d1ah ;0d0b
+ push bc ;0d0d
+ add hl,hl ;0d0e hl *= 10
+ ld b,h ;0d0f
+ ld c,l ;0d10
+ add hl,hl ;0d11
+ add hl,hl ;0d12
+ add hl,bc ;0d13
+ pop bc ;0d14
+ call ADD_HL_A ;0d15
+ jr l0d07h ;0d18
+l0d1ah:
+ cp '.' ;0d1a
+ ret z ;0d1c
+ jp ERROR ;0d1d
+
+sub_0d20h:
+ ld a,(de) ;0d20
+ cp 05bh ;0d21
+ jr l0d28h ;0d23
+
+get.hexdigit:
+ ld a,(de) ;0d25
+sub_0d26h:
+ cp 'F'+1 ;0d26
+l0d28h:
+ ccf ;0d28
+ ret c ;0d29
+ cp 'A' ;0d2a
+ jr c,l0d32h ;0d2c
+ sub 'A'-10 ;0d2e
+ ret ;0d30
+
+get.decdigit:
+ ld a,(de) ;0d31
+l0d32h:
+ cp '9'+1 ;0d32
+ jr l0d39h ;0d34
+get.bindigit:
+ ld a,(de) ;0d36
+ cp '1'+1 ;0d37
+l0d39h:
+ ccf ;0d39
+ ret c ;0d3a
+ cp '0' ;0d3b
+ ret c ;0d3d
+ sub '0' ;0d3e
+ ret ;0d40
+
+l0d41h:
+ call assert_eol ;0d41
+
+prnt_cpustat:
+ call prnt_f ;0d44
+ call outbl2 ;0d47
+ ld hl,b_0x0DFD_start ;0d4a
+ ld de,b_0x0E1D_start ;0d4d
+ ld b,006h ;0d50
+l0d52h:
+ call prnt_regs ;0d52
+ djnz l0d52h ;0d55
+ push hl ;0d57
+ push de ;0d58
+ ld iy,(REG.PC) ;0d59
+ call sub_1f77h ;0d5d
+ exx ;0d60
+ ex af,af' ;0d61
+ call CRLF ;0d62
+ call prnt_f2 ;0d65
+ call outbl2 ;0d68
+ pop de ;0d6b
+ pop hl ;0d6c
+ ld b,007h ;0d6d
+l0d6fh:
+ call prnt_regs ;0d6f
+ djnz l0d6fh ;0d72
+ exx ;0d74
+ ex af,af' ;0d75
+ and a ;0d76
+ jr z,l0d7fh ;0d77
+ call outbl6 ;0d79
+ call sub_1f5bh ;0d7c
+l0d7fh:
+ jp crlf ;0d7f
+
+prnt_f:
+ ld a,(reg.f) ;0d82
+ call prnt_flags ;0d85
+ ld a,(reg.iff) ;0d88
+ cp 0f3h ;0d8b
+ jp z,outbl ;0d8d
+ ld a,'E' ;0d90
+ jp outchar ;0d92
+
+prnt_f2:
+ ld a,(reg.f2) ;0d95
+ call prnt_flags ;0d98
+ jp outbl ;0d9b
+
+prnt_flags:
+ ld b,a ;0d9e
+ ld a,'S' ;0d9f
+ call sub_0dbeh ;0da1
+ ld a,'Z' ;0da4
+ call sub_0dbeh ;0da6
+ rl b ;0da9
+ ld a,'H' ;0dab
+ call sub_0dbeh ;0dad
+ rl b ;0db0
+ ld a,'V' ;0db2
+ call sub_0dbeh ;0db4
+ ld a,'N' ;0db7
+ call sub_0dbeh ;0db9
+ ld a,'C' ;0dbc
+sub_0dbeh:
+ rl b ;0dbe
+ jp c,OUTCHAR ;0dc0
+ jp OUTBL ;0dc3
+
+prnt_regs:
+ push bc ;0dc6
+ push de ;0dc7
+ call PSTR ;0dc8
+ ld a,'=' ;0dcb
+ call OUTCHAR ;0dcd
+ ex (sp),hl ;0dd0
+ ld e,(hl) ;0dd1
+ inc hl ;0dd2
+ ld d,(hl) ;0dd3
+ inc hl ;0dd4
+ ld a,(hl) ;0dd5
+ inc hl ;0dd6
+ push hl ;0dd7
+ and a ;0dd8
+ jr z,l0df2h ;0dd9
+ push af ;0ddb
+ ld a,(de) ;0ddc
+ ld l,a ;0ddd
+ inc de ;0dde
+ ld a,(de) ;0ddf
+ ld h,a ;0de0
+ pop af ;0de1
+ dec a ;0de2
+ jr z,l0dedh ;0de3
+ call out.hl.@ ;0de5
+ call z,outbl6 ;0de8
+ jr l0df6h ;0deb
+l0dedh:
+ call out.hl ;0ded
+ jr l0df6h ;0df0
+l0df2h:
+ ld a,(de) ;0df2
+ call out.hex ;0df3
+l0df6h:
+ call OUTBL ;0df6
+ pop de ;0df9
+ pop hl ;0dfa
+ pop bc ;0dfb
+ ret ;0dfc
+
+b_0x0DFD_start:
+ DC 'A '
+ DC 'BC '
+ DC 'DE '
+ DC 'HL '
+ DC 'SP'
+ DC 'PC'
+ DC 'A'''
+ DC 'BC'''
+ DC 'DE'''
+ DC 'HL'''
+ DC 'IX'
+ DC 'IY'
+ DC 'I'
+ DB 0
+
+b_0x0E1D_start:
+ defw reg.a ;0e1d
+ defb 000h ;0e1f
+ defw reg.c ;0e20
+ defb 001h ;0e22
+ defw reg.e ;0e23
+ defb 001h ;0e25
+ defw reg.l ;0e26
+ defb 001h ;0e28
+ defw reg.sp ;0e29
+ defb 001h ;0e2b
+ defw reg.pc ;0e2c
+ defb 002h ;0e2e
+ defw reg.a2 ;0e2f
+ defb 000h ;0e31
+ defw reg.c2 ;0e32
+ defb 001h ;0e34
+ defw reg.e2 ;0e35
+ defb 001h ;0e37
+ defw reg.l2 ;0e38
+ defb 001h ;0e3a
+ defw reg.ix ;0e3b
+ defb 001h ;0e3d
+ defw reg.iy ;0e3e
+ defb 001h ;0e40
+ defw reg.i ;0e41
+ dw 0 ;0e43
+
+CMD.G:
+ sub a ;0e45
+ ld (TCFLG),a ;0e46
+ ld (XA747),a ;0e49
+ call EXPR ;0e4c
+ jr c,l0e54h ;0e4f
+ ld (REG.PC),hl ;0e51
+l0e54h:
+ call SKIPBL ;0e54
+ jp z,l1183h ;0e57
+ cp ';' ;0e5a
+ jp nz,ERROR ;0e5c
+ inc de ;0e5f
+ ld a,002h ;0e60
+ call sub_0f24h ;0e62
+ jp l1183h ;0e65
+
+sub_0e68h:
+ ld b,BP_CNT ;0e68
+ ld ix,bp_tab ;0e6a
+l0e6eh:
+ ld a,(ix+000h) ;0e6e
+ and 0f1h ;0e71
+ ld (ix+000h),a ;0e73
+ call sub_11c5h ;0e76
+ ld de,BP_SIZE ;0e79
+ add ix,de ;0e7c
+ djnz l0e6eh ;0e7e
+ ret ;0e80
+
+CMD.B:
+ call SKIPBL ;0e81
+ jr z,l0ecbh ;0e84
+ inc de ;0e86
+ cp 'X' ;0e87
+ jr z,l0e91h ;0e89
+ dec de ;0e8b
+ ld a,001h ;0e8c
+ jp sub_0f24h ;0e8e
+l0e91h:
+ call SKIPBL ;0e91
+ jr z,l0ea6h ;0e94
+l0e96h:
+ call EXPR ;0e96
+ jp c,assert_eol ;0e99
+ push de ;0e9c
+ call sub_0ea7h ;0e9d
+ pop de ;0ea0
+ call skip_to_nextarg ;0ea1
+ jr l0e96h ;0ea4
+l0ea6h:
+ scf ;0ea6
+sub_0ea7h:
+ ld b,BP_CNT ;0ea7
+ ld ix,bp_tab ;0ea9
+l0eadh:
+ push af ;0ead
+ jr c,l0ebbh ;0eae
+ ld e,(ix+002h) ;0eb0
+ ld d,(ix+003h) ;0eb3
+ call CP.HL.DE ;0eb6
+ jr nz,l0ec2h ;0eb9
+l0ebbh:
+ ld (ix+000h),000h ;0ebb
+ call sub_11c5h ;0ebf
+l0ec2h:
+ ld de,BP_SIZE ;0ec2
+ add ix,de ;0ec5
+ pop af ;0ec7
+ djnz l0eadh ;0ec8
+ ret ;0eca
+l0ecbh:
+ ld b,BP_CNT ;0ecb
+ ld ix,bp_tab ;0ecd
+l0ed1h:
+ bit 0,(ix+000h) ;0ed1
+ jr z,l0f1ch ;0ed5
+ ld a,'R' ;0ed7
+ bit 4,(ix+000h) ;0ed9
+ jr nz,l0ee1h ;0edd
+ ld a,' ' ;0edf
+l0ee1h:
+ call OUTCHAR ;0ee1
+ call OUTBL ;0ee4
+ ld l,(ix+002h) ;0ee7
+ ld h,(ix+003h) ;0eea
+ call out.hl.@ ;0eed
+ call outbl2 ;0ef0
+ ld a,':' ;0ef3
+ call OUTCHAR ;0ef5
+ ld l,(ix+004h) ;0ef8
+ ld h,(ix+005h) ;0efb
+ call out.hl ;0efe
+ ld l,(ix+006h) ;0f01
+ ld h,(ix+007h) ;0f04
+ ld a,h ;0f07
+ or l ;0f08
+ jr z,l0f19h ;0f09
+ call outbl4 ;0f0b
+ ld a,'I' ;0f0e
+ call OUTCHAR ;0f10
+ call outbl2 ;0f13
+ call PSTR ;0f16
+l0f19h:
+ call CRLF ;0f19
+l0f1ch:
+ ld de,BP_SIZE ;0f1c
+ add ix,de ;0f1f
+ djnz l0ed1h ;0f21
+ ret ;0f23
+
+sub_0f24h:
+ ld b,a ;0f24
+ call SKIPBL ;0f25
+ ret z ;0f28
+ cp 'R' ;0f29
+ jr nz,l0f30h ;0f2b
+ inc de ;0f2d
+ set 4,b ;0f2e
+l0f30h:
+ push bc ;0f30
+ call EXPR ;0f31
+ jp c,ERROR ;0f34
+ pop bc ;0f37
+ bit 0,b ;0f38
+ push bc ;0f3a
+ push de ;0f3b
+ push hl ;0f3c
+ call nz,sub_0ea7h ;0f3d
+ pop hl ;0f40
+ call sub_0f68h ;0f41
+ pop de ;0f44
+ ld (ix+002h),l ;0f45
+ ld (ix+003h),h ;0f48
+ call sub_0f80h ;0f4b
+ ld (ix+004h),l ;0f4e
+ ld (ix+005h),h ;0f51
+ call sub_0f91h ;0f54
+ ld (ix+006h),l ;0f57
+ ld (ix+007h),h ;0f5a
+ call skip_to_nextarg ;0f5d
+ pop af ;0f60
+ ld (ix+000h),a ;0f61
+ and 00fh ;0f64
+ jr sub_0f24h ;0f66
+sub_0f68h:
+ ld b,BP_CNT ;0f68
+ ld ix,bp_tab ;0f6a
+l0f6eh:
+ ld a,(ix+000h) ;0f6e
+ and 00fh ;0f71
+ ret z ;0f73
+ push bc ;0f74
+ ld bc,BP_SIZE ;0f75
+ add ix,bc ;0f78
+ pop bc ;0f7a
+ djnz l0f6eh ;0f7b
+ jp ERROR ;0f7d
+
+sub_0f80h:
+ call SKIPBL ;0f80
+ ld hl,1 ;0f83
+ cp 03ah ;0f86
+ ret nz ;0f88
+ inc de ;0f89
+ call EXPR ;0f8a
+ jp c,ERROR ;0f8d
+ ret ;0f90
+sub_0f91h:
+ call SKIPBL ;0f91
+ cp 049h ;0f94
+ ld hl,0 ;0f96
+ ret nz ;0f99
+ inc de ;0f9a
+ call SKIPBL ;0f9b
+ push de ;0f9e
+ call EXPR ;0f9f
+ jp c,ERROR ;0fa2
+ ex de,hl ;0fa5
+ pop de ;0fa6
+ push de ;0fa7
+ sbc hl,de ;0fa8
+ ld b,h ;0faa
+ ld c,l ;0fab
+ ld hl,(sexp1) ;0fac
+ push hl ;0faf
+ add hl,bc ;0fb0
+ ld de,sexpbufe ;0fb1
+ call CP.HL.DE ;0fb4
+ jp nc,ERROR ;0fb7
+ pop hl ;0fba
+ ld (sexp2),hl ;0fbb
+ pop de ;0fbe
+ ex de,hl ;0fbf
+ ldir ;0fc0
+ xor a ;0fc2
+ ld (de),a ;0fc3
+ inc de ;0fc4
+ ex de,hl ;0fc5
+ ld (sexp1),hl ;0fc6
+ ld hl,(sexp2) ;0fc9
+ ret ;0fcc
+
+bpddtz:
+ if ROMSYS
+ ld h,ROMEN ;0fcd
+ jr z,l0fd2h ;0fcf
+ inc h ;0fd1
+l0fd2h:
+ push hl ;0fd2 save rom enable stat
+ endif
+ push bc ;0fd3
+ push de ;0fd4
+ push ix ;0fd5
+ push iy ;0fd7
+ ld a,i ;0fd9
+ ld h,a ;0fdb
+ ld l,000h ;0fdc
+ push hl ;0fde
+ ld a,0f3h ;0fdf DI
+ jp po,l0fe6h ;0fe1
+ ld a,0fbh ;0fe4 EI
+l0fe6h:
+ ld (reg.iff),a ;0fe6
+ ld hl,ivtab ;0fe9
+ ld a,h ;0fec
+ ld i,a ;0fed
+ call ddtei ;0fef
+ ex af,af' ;0ff2
+ push af ;0ff3
+ exx ;0ff4
+ push bc ;0ff5
+ push de ;0ff6
+ push hl ;0ff7
+ call bp.unset ;0ff8
+ in0 a,(itc) ;0ffb
+ jp p,l1017h ;0ffe
+ res TRAP,a ;1001
+ out0 (itc),a ;1003
+ bit UFO,a ;1006
+ jr z,l1011h ;1008
+ ld hl,(REG.PC) ;100a
+ dec hl ;100d
+ ld (REG.PC),hl ;100e
+l1011h:
+ ld hl,MSG_trap ;1011
+ call PSTR ;1014
+l1017h:
+ ld a,(XBFE8) ;1017
+ dec a ;101a
+ jr z,l1051h ;101b
+ call inchar ;101d
+ jr c,l102eh ;1020
+ call sub_1059h ;1022
+ and a ;1025
+ jp z,l1183h ;1026
+ and 083h ;1029
+ jp z,l284ah ;102b
+l102eh:
+ call sub_0e68h ;102e
+ call prnt_cpustat ;1031
+ jp DDTZML ;1034
+
+MSG_trap:
+ DB CR,LF,'Undefined opcode trap'
+ DB CR,LF,0
+
+l1051h:
+ ld (XBFE8),a ;1051
+ ld c,007h ;1054
+ jp l119fh ;1056
+sub_1059h:
+ ld a,080h ;1059
+ ex af,af' ;105b
+ sub a ;105c
+ ld (XA747),a ;105d
+ ld b,BP_CNT ;1060
+ ld ix,bp_tab ;1062
+l1066h:
+ ld a,(ix+000h) ;1066
+ and 007h ;1069
+ jr z,l107eh ;106b
+ ld e,(ix+002h) ;106d
+ ld d,(ix+003h) ;1070
+ ld hl,(REG.PC) ;1073
+ call CP.HL.DE ;1076
+ push bc ;1079
+ call z,sub_1087h ;107a
+ pop bc ;107d
+l107eh:
+ ld de,BP_SIZE ;107e
+ add ix,de ;1081
+ djnz l1066h ;1083
+ ex af,af' ;1085
+ ret ;1086
+sub_1087h:
+ ex af,af' ;1087
+ res 7,a ;1088
+ ex af,af' ;108a
+ ld e,(ix+006h) ;108b
+ ld d,(ix+007h) ;108e
+ ld a,d ;1091
+ or e ;1092
+ ld hl,0ffffh ;1093
+ call nz,EXPR ;1096
+ ld a,h ;1099
+ or l ;109a
+ jr z,l10aeh ;109b
+ ld e,(ix+004h) ;109d
+ ld d,(ix+005h) ;10a0
+ dec de ;10a3
+ ld a,d ;10a4
+ or e ;10a5
+ jr z,l10b9h ;10a6
+ ld (ix+004h),e ;10a8
+ ld (ix+005h),d ;10ab
+l10aeh:
+ bit 4,(ix+000h) ;10ae
+ ret z ;10b2
+ ld a,001h ;10b3
+ ld (XA747),a ;10b5
+ ret ;10b8
+l10b9h:
+ ex af,af' ;10b9
+ or (ix+000h) ;10ba
+ ex af,af' ;10bd
+ ret
+
+bp.unset:
+ ld b,BP_CNT ;10bf
+ ld ix,bp_tab ;10c1
+l10c5h:
+ bit 5,(ix+000h) ;10c5
+ res 5,(ix+000h) ;10c9
+ jr z,l10e7h ;10cd
+ ld l,(ix+002h) ;10cf
+ ld h,(ix+003h) ;10d2
+ ld a,(ddtrst) ;10d5
+ comst ;10e2
+ cp (hl) ;10dc
+ comend
+ jr nz,l10e7h ;10dd
+ ld a,(ix+001h) ;10df
+ comst ;10e2
+ ld (hl),a ;10e6
+ comend
+l10e7h:
+ res 3,(ix+000h) ;10e7
+ ld de,BP_SIZE ;10eb
+ add ix,de ;10ee
+ djnz l10c5h ;10f0
+ ret ;10f2
+sub_10f3h:
+ ld b,BP_CNT ;10f3
+ ld ix,bp_tab ;10f5
+l10f9h:
+ ld a,(ix+000h) ;10f9
+ and 003h ;10fc
+ jr z,l110dh ;10fe
+ ld e,(ix+002h) ;1100
+ ld d,(ix+003h) ;1103
+ ld hl,(REG.PC) ;1106
+ call CP.HL.DE ;1109
+ ret z ;110c
+l110dh:
+ ld de,BP_SIZE ;110d
+ add ix,de ;1110
+ djnz l10f9h ;1112
+ sub a ;1114
+ inc a ;1115
+ ret ;1116
+sub_1117h:
+ call sub_0f68h ;1117
+ ld (ix+004h),001h ;111a
+ ld (ix+005h),000h ;111e
+ ld (ix+002h),l ;1122
+ ld (ix+003h),h ;1125
+ ld (ix+006h),000h ;1128
+ ld (ix+007h),000h ;112c
+ ld a,(XBFE8) ;1130
+ and a ;1133
+ ld a,008h ;1134
+ jr nz,l113ah ;1136
+ ld a,004h ;1138
+l113ah:
+ ld (ix+000h),a ;113a
+ ret ;113d
+
+bp.set:
+ ld b,BP_CNT ;113e
+ ld ix,bp_tab ;1140
+l1144h:
+ ld a,(ix+000h) ;1144
+ and c ;1147
+ jr z,l117bh ;1148
+ set 5,(ix+000h) ;114a
+ ld l,(ix+002h) ;114e
+ ld h,(ix+003h) ;1151
+
+ if 0
+
+ comst ;1154
+ ld a,(hl) ;1158
+ comend
+ ld (ix+001h),a ;1159
+ ld a,(ddtrst) ;115c
+ comst ;115f
+ ld (hl),a ;1163
+ comend
+ and 038h ;1164
+ ld h,000h ;1166
+ ld l,a ;1168
+ ld a,0c3h ;1169
+ comrep ;116b
+ inc hl ;116e
+ ld de,bpent ;116f
+ ld a,e ;1172
+ comrep ;1173
+ inc hl ;1176
+ ld a,d ;1177
+ comrep ;1178
+
+ else
+
+ ld a,(ddtrst) ;115c
+ comst ;115f
+ ld e,(hl) ;1158
+ ld (hl),a ;1163
+ comend
+ ld (ix+001h),e ;1159
+ and 038h ;1164
+ ld h,0 ;1166
+ ld l,a ;1168
+ ld de,bpent ;116f
+ comst ;
+ ld (hl),0c3h ;1169
+ inc hl ;116e
+ ld (hl),e ;1172
+ inc hl ;1176
+ ld (hl),d ;1177
+ comend
+
+ endif
+
+l117bh:
+ ld de,BP_SIZE ;117b
+ add ix,de ;117e
+ djnz l1144h ;1180
+ ret
+ ;1182
+l1183h:
+ sub a ;1183
+ ld (XBFE8),a ;1184
+ ld a,(XA747) ;1187
+ and a ;118a
+ call nz,prnt_cpustat ;118b
+ call sub_10f3h ;118e
+ ld c,007h ;1191
+ jr nz,l119fh ;1193
+ ld a,001h ;1195
+ ld (XBFE8),a ;1197
+ call sub_26e7h ;119a
+ ld c,008h ;119d
+l119fh:
+ call bp.set ;119f
+ ld sp,$stack ;11a2 set/restore user cpu state
+ pop hl ;11a5
+ pop de ;11a6
+ pop bc ;11a7
+ pop af ;11a8
+ exx ;11a9
+ ex af,af' ;11aa
+ pop af ;11ab
+ ld i,a ;11ac
+ pop iy ;11ae
+ pop ix ;11b0
+ pop de ;11b2
+ pop bc ;11b3
+ if ROMSYS
+ pop hl ;11b4
+ ld a,l ;11b5
+ and M_MWI ;11b6
+ ld l,a ;11b8
+ di ;11b9
+ in0 a,(dcntl) ;11ba
+ and ~M_MWI ;11bd
+ or l ;11bf
+ ld l,a ;11c0
+ ld a,h ;11c1
+ else
+ pop hl
+ di
+ endif
+ jp $go ;11c2 common ram, switch banks and go to user prog
+
+sub_11c5h:
+ ld a,(ix+000h) ;11c5
+ and 003h ;11c8
+ ret nz ;11ca
+ ld e,(ix+006h) ;11cb
+ ld d,(ix+007h) ;11ce
+ ld a,d ;11d1
+ or e ;11d2
+ ret z ;11d3
+ push bc ;11d4
+ ld h,d ;11d5
+ ld l,e ;11d6
+ sub a ;11d7
+ ld (ix+006h),a ;11d8
+ ld (ix+007h),a ;11db
+ ld bc,0ffffh ;11de
+ cpir ;11e1
+l11e3h:
+ push de ;11e3
+ ld de,(sexp1) ;11e4
+ call CP.HL.DE ;11e8
+ pop de ;11eb
+ jr nc,l11f9h ;11ec
+ call sub_11ffh ;11ee
+l11f1h:
+ ld a,(hl) ;11f1
+ ldi ;11f2
+ and a ;11f4
+ jr nz,l11f1h ;11f5
+ jr l11e3h ;11f7
+l11f9h:
+ ld (sexp1),de ;11f9
+ pop bc ;11fd
+ ret ;11fe
+sub_11ffh:
+ ld iy,bp_tab ;11ff
+ push de ;1203
+l1204h:
+ ld e,(iy+006h) ;1204
+ ld d,(iy+007h) ;1207
+ call CP.HL.DE ;120a
+ jr z,l1216h ;120d
+ ld de,BP_SIZE ;120f
+ add iy,de ;1212
+ jr l1204h ;1214
+l1216h:
+ pop de ;1216
+ ld (iy+006h),e ;1217
+ ld (iy+007h),d ;121a
+ ret ;121d
+
+CMD.Y:
+ call get.decdigit ;121e
+ jr c,l122fh ;1221
+ inc de ;1223
+ push af ;1224
+ call assert_eol ;1225
+ pop af ;1228
+ call sub_1248h ;1229
+ jp l127ch ;122c
+l122fh:
+ call assert_eol ;122f
+ xor a ;1232
+l1233h:
+ push af ;1233
+ call sub_1248h ;1234
+ call outbl4 ;1237
+ pop af ;123a
+ inc a ;123b
+ bit 0,a ;123c
+ push af ;123e
+ call z,CRLF ;123f
+ pop af ;1242
+ cp LF ;1243
+ jr c,l1233h ;1245
+ ret ;1247
+
+sub_1248h:
+ ld c,a ;1248
+ ld b,0 ;1249
+ add a,'0'+080h ;124b
+ ld de,msg.Y+1 ;124d
+ ld (de),a ;1250
+ dec de ;1251
+ ld hl,reg.Y ;1252
+ add hl,bc ;1255
+ add hl,bc ;1256
+ ex de,hl ;1257
+ ld c,003h ;1258
+ jp l129ah ;125a
+
+CMD.X:
+ call SKIPBL ;125d
+ call sub_1315h ;1260
+ jp nc,l0d41h ;1263
+ call assert_eol ;1266
+ ld a,b ;1269
+ cp 01fh ;126a
+ jr z,l12c6h ;126c
+ cp 020h ;126e
+ jr z,l12b6h ;1270
+ ex de,hl ;1272
+ ld hl,b_0x132A_start ;1273
+ call sub_0a48h ;1276
+l1279h:
+ call l129ah ;1279
+l127ch:
+ call OUTBL ;127c
+ push de ;127f
+ push bc ;1280
+ call INLINE ;1281
+ call SKIPBL ;1284
+ jr z,l1297h ;1287
+ call get_arg ;1289
+ ld b,h ;128c
+ ld c,l ;128d
+ pop af ;128e
+ pop hl ;128f
+ ld (hl),c ;1290
+ bit 0,a ;1291
+ ret z ;1293
+ inc hl ;1294
+ ld (hl),b ;1295
+ ret ;1296
+l1297h:
+ pop af ;1297
+ pop hl ;1298
+ ret ;1299
+l129ah:
+ ld b,c ;129a
+ call PSTR ;129b
+ ld a,'=' ;129e
+ call OUTCHAR ;12a0
+ ld a,(de) ;12a3
+ bit 0,b ;12a4
+ jp z,out.hex ;12a6
+ ld l,a ;12a9
+ inc de ;12aa
+ ld a,(de) ;12ab
+ dec de ;12ac
+ ld h,a ;12ad
+ bit 1,b ;12ae
+ jp z,out.hl ;12b0
+ jp out.hl.@ ;12b3
+
+l12b6h:
+ call prnt_f ;12b6
+ ld a,0f3h ;12b9
+ ld (reg.iff),a ;12bb
+ scf ;12be
+ call sub_12d1h ;12bf
+ ld (reg.f),a ;12c2
+ ret ;12c5
+
+l12c6h:
+ call prnt_f2 ;12c6
+ and a ;12c9
+ call sub_12d1h ;12ca
+ ld (reg.f2),a ;12cd
+ ret ;12d0
+
+sub_12d1h:
+ ex af,af' ;12d1
+ ld b,000h ;12d2
+ call outbl ;12d4
+ call assert_eol ;12d7
+ call inline ;12da
+l12ddh:
+ call skipbl ;12dd
+ ld a,b ;12e0
+ ret z ;12e1
+ push bc ;12e2
+ ld hl,tab_pr_flags ;12e3
+ call lookupch ;12e6
+ jp nc,error ;12e9
+ ld a,b ;12ec
+ cp 008h ;12ed
+ jr z,l12feh ;12ef
+ inc b ;12f1
+ ld a,001h ;12f2
+ jr l12f7h ;12f4
+l12f6h:
+ rlca ;12f6
+l12f7h:
+ djnz l12f6h ;12f7
+ pop bc ;12f9
+ or b ;12fa
+ ld b,a ;12fb
+ jr l12ddh ;12fc
+l12feh:
+ ex af,af' ;12fe
+ jp nc,ERROR ;12ff
+ ex af,af' ;1302
+ ld a,0FBh ;1303
+ ld (reg.iff),a ;1305
+ pop bc ;1308
+ jr l12ddh ;1309
+
+tab_pr_flags:
+ db 'CNV H ZSE'
+ db 0
+
+sub_1315h:
+ call sub_0a0eh ;1315
+ ret nc ;1318
+ ld a,b ;1319
+ add a,b ;131a
+ add a,b ;131b
+ ld hl,b_0x136C_start ;131c
+ call ADD_HL_A ;131f
+ ld c,(hl) ;1322
+ inc hl ;1323
+ ld a,(hl) ;1324
+ inc hl ;1325
+ ld h,(hl) ;1326
+ ld l,a ;1327
+ scf ;1328
+ ret ;1329
+
+b_0x132A_start:
+ DC 'CBAR'
+ DC 'BBR'
+ DC 'BC'''
+ DC 'DE'''
+ DC 'HL'''
+ DC 'BC'
+ DC 'DE'
+ DC 'HL'
+ DC 'A'''
+ DC 'B'''
+ DC 'C'''
+ DC 'D'''
+ DC 'E'''
+ DC 'H'''
+ DC 'L'''
+ DC 'A'
+ DC 'B'
+ DC 'C'
+ DC 'D'
+ DC 'E'
+ DC 'H'
+ DC 'L'
+ DC 'IX'
+ DC 'IY'
+ DC 'SP'
+ DC 'PC'
+ DC 'X'
+ DC 'Y'
+ DC 'S'
+ DC 'P'
+ DC 'I'
+ DC 'F'''
+ DC 'F'
+ if ROMSYS
+ DC 'ROMSEL'
+ endif
+ DB 0
+b_0x136C_start:
+ defb 000h ;136c
+ defw ucbar ;136d
+ defb 000h ;136f
+ defw ubbr ;1370
+ defb 003h ;1372
+ defw reg.c2 ;1373
+ defb 003h ;1375
+ defw reg.e2 ;1376
+ defb 003h ;1378
+ defw reg.l2 ;1379
+ defb 003h ;137b
+ defw reg.c ;137c
+ defb 003h ;137e
+ defw reg.e ;137f
+ defb 003h ;1381
+ defw reg.l ;1382
+ defb 000h ;1384
+ defw reg.a2 ;1385
+ defb 000h ;1387
+ defw reg.b2 ;1388
+ defb 000h ;138a
+ defw reg.c2 ;138b
+ defb 000h ;138d
+ defw reg.d2 ;138e
+ defb 000h ;1390
+ defw reg.e2 ;1391
+ defb 000h ;1393
+ defw reg.h2 ;1394
+ defb 000h ;1396
+ defw reg.l2 ;1397
+ defb 000h ;1399
+ defw reg.a ;139a
+ defb 000h ;139c
+ defw reg.b ;139d
+ defb 000h ;139f
+ defw reg.c ;13a0
+ defb 000h ;13a2
+ defw reg.d ;13a3
+ defb 000h ;13a5
+ defw reg.e ;13a6
+ defb 000h ;13a8
+ defw reg.h ;13a9
+ defb 000h ;13ab
+ defw reg.l ;13ac
+ defb 003h ;13ae
+ defw reg.ix ;13af
+ defb 003h ;13b1
+ defw reg.iy ;13b2
+ defb 003h ;13b4
+ defw reg.sp ;13b5
+ defb 003h ;13b7
+ defw reg.pc ;13b8
+ defb 003h ;13ba
+ defw reg.ix ;13bb
+ defb 003h ;13bd
+ defw reg.iy ;13be
+ defb 003h ;13c0
+ defw reg.sp ;13c1
+ defb 003h ;13c3
+ defw reg.pc ;13c4
+ defb 000h ;13c6
+ defw reg.i ;13c7
+ defb 000h ;13c9
+ defw reg.f2 ;13ca
+ defb 000h ;13cc
+ defw reg.f ;13cd
+ if ROMSYS
+ defb 000h ;13cf
+ defw uromen ;13d0
+ endif
+CMD.S:
+ ld hl,(lst.S) ;13d2
+ call get_lastarg_def ;13d5
+l13d8h:
+ ld (lst.S),hl ;13d8
+ call out.hl.@ ;13db
+ call OUTBL ;13de
+ comst ;13e1
+ ld a,(hl) ;13e5
+ comend
+ call out.hex ;13e6
+ call outbl2 ;13e9
+ call INLINE ;13ec
+ call SKIPBL ;13ef
+ inc hl ;13f2
+ jr z,l13d8h ;13f3
+ dec hl ;13f5
+ inc de ;13f6
+ cp '.' ;13f7
+ jp z,assert_eol ;13f9
+ cp '-' ;13fc
+ jr nz,l1406h ;13fe
+ ld a,(de) ;1400
+ or a ;1401
+ dec hl ;1402
+ jr z,l13d8h ;1403
+ inc hl ;1405
+l1406h:
+ dec de ;1406
+ call get_bytes_m ;1407
+ jr l13d8h ;140a
+
+CMD.@:
+ call assert_eol ;140c
+ ld hl,MSG_at ;140f
+ ld de,offs.@ ;1412
+ ld c,001h ;1415
+ jp l1279h ;1417
+MSG_at:
+ dc '@'
+
+CMD.I:
+ ld hl,CMD.I ;141b
+ ld (CMD_RPT),hl ;141e
+ ld hl,(lst.IP) ;1421
+ call get_lastarg_def ;1424
+ ld (lst.IP),hl ;1427
+ ld b,h ;142a
+ ld c,l ;142b
+ ld a,b ;142c
+ or a ;142d
+ jr nz,l1442h ;142e
+ ld a,c ;1430
+ ld hl,ucbar ;1431
+ cp cbar ;1434
+ jr z,l143fh ;1436
+ ld hl,ubbr ;1438
+ cp bbr ;143b
+ jr nz,l1442h ;143d
+l143fh:
+ ld a,(hl) ;143f
+ jr l1444h ;1440
+l1442h:
+ in a,(c) ;1442
+l1444h:
+ push af ;1444
+ call out.hex ;1445
+ call outbl4 ;1448
+ pop af ;144b
+ call out.bin.b ;144c
+ jp CRLF ;144f
+CMD.O:
+ ld hl,CMD.O ;1452
+ ld (CMD_RPT),hl ;1455
+ ld hl,(lst.OD) ;1458
+ call get_arg_def ;145b
+ ld a,l ;145e
+ ld (lst.OD),a ;145f
+ push af ;1462
+ call skip_to_nextarg ;1463
+ ld hl,(lst.OP) ;1466
+ call get_lastarg_def ;1469
+ ld (lst.OP),hl ;146c
+ ld b,h ;146f
+ ld c,l ;1470
+ ld a,b ;1471
+ or a ;1472
+ jr nz,l1489h ;1473
+ ld a,c ;1475
+ ld hl,ucbar ;1476
+ cp cbar ;1479
+ jr z,l148dh ;147b
+ ld hl,ubbr ;147d
+ cp bbr ;1480
+ jr z,l148dh ;1482
+ cp cbr ;1484
+ jp z,ERROR ;1486
+l1489h:
+ pop af ;1489
+ out (c),a ;148a
+ ret ;148c
+l148dh:
+ pop af ;148d
+ ld (hl),a ;148e
+ ret ;148f
+
+CMD.V:
+ call get_arg3 ;1490 get from, size, to
+cmp_mem:
+ push bc ;1493
+ comst ;1494
+ ld a,(de) ;1498
+ ld b,(hl) ;1499
+ comend
+ cp b ;149a
+ jr z,l14bah ;149b
+ ld c,a ;149d
+ call out.hl.@ ;149e
+ call OUTBL ;14a1
+ ld a,b ;14a4
+ call out.hex ;14a5
+ call outbl2 ;14a8
+ ld a,c ;14ab
+ call out.hex ;14ac
+ call OUTBL ;14af
+ ex de,hl ;14b2
+ call out.hl.@ ;14b3
+ ex de,hl ;14b6
+ call CRLF ;14b7
+l14bah:
+ pop bc ;14ba
+ inc hl ;14bb
+ inc de ;14bc
+ dec bc ;14bd
+ ld a,b ;14be
+ or c ;14bf
+ jr nz,cmp_mem ;14c0
+ ret ;14c2
+
+CMD.M:
+ ld a,(de) ;14c3
+ cp 'V' ;14c4
+ jr nz,bm_nv ;14c6
+ inc de ;14c8
+bm_nv:
+ push af ;14c9 save 'V' flag
+ call get_arg3 ;14ca
+ push hl ;14cd
+ push de ;14ce
+ push bc ;14cf
+ call CP.HL.DE ;14d0
+ jr nc,bm_mvdown ;14d3
+ add hl,bc ;14d5
+ ex de,hl ;14d6
+ add hl,bc ;14d7
+ ex de,hl ;14d8
+ dec hl ;14d9
+ dec de ;14da
+ comst ;14db
+ lddr ;14df
+ comend
+ jr bm_done ;14e1
+bm_mvdown:
+ comst ;14e3
+ ldir ;14e7
+ comend
+bm_done:
+ pop bc ;14e9
+ pop de ;14ea
+ pop hl ;14eb
+ pop af ;14ec
+ jr z,cmp_mem ;14ed validate?
+ ret ;14ef
+CMD.H:
+ call EXPR ;14f0
+ jp c,l173ch ;14f3
+ call skip_to_nextarg ;14f6
+ push hl ;14f9
+ call EXPR ;14fa
+ push af ;14fd
+ call assert_eol ;14fe
+ pop af ;1501
+ ex de,hl ;1502
+ pop hl ;1503
+ jr c,l1511h ;1504
+ push hl ;1506
+ push de ;1507
+ add hl,de ;1508
+ call l1511h ;1509
+ pop de ;150c
+ pop hl ;150d
+ and a ;150e
+ sbc hl,de ;150f
+l1511h:
+ call out.hl ;1511 val
+ call outbl2 ;1514
+ call sub_0928h ;1517 -val
+ call outbl4 ;151a
+ call out.hl.dec ;151d dec
+ call outbl2 ;1520
+ call out.hl.decm ;1523 -dec
+ call outbl4 ;1526
+ call out.bin.w ;1529 bin
+ call outbl2 ;152c
+ ld a,l ;152f
+ call out.ascii ;1530
+ jp CRLF ;1533
+
+CMD.Q:
+ ld a,(de) ;1536
+ sub 'J' ;1537
+ ld (lst.Qj),a ;1539
+ jr nz,l153fh ;153c
+ inc de ;153e
+l153fh:
+ call get_arg_range ;153f
+ push bc ;1542
+ push hl ;1543
+ call sub_15a7h ;1544
+ pop hl ;1547
+l1548h:
+ call sub_1594h ;1548
+ jr nz,l1562h ;154b
+ push bc ;154d
+ push hl ;154e
+ ld a,(lst.Qj) ;154f
+ or a ;1552
+ jr nz,l1559h ;1553
+ ld bc,-8 ;1555
+ add hl,bc ;1558
+l1559h:
+ ld bc,MEMDUMP_CNT ;1559
+ and a ;155c
+ call memdump ;155d
+ pop hl ;1560
+ pop bc ;1561
+l1562h:
+ inc hl ;1562
+ ex (sp),hl ;1563
+ dec hl ;1564
+ ld a,h ;1565
+ or l ;1566
+ ex (sp),hl ;1567
+ jr nz,l1548h ;1568
+ pop bc ;156a
+ ret ;156b
+
+CMD.Z:
+ call get_arg_range ;156c
+ push bc ;156f
+ push hl ;1570
+ call sub_15a7h ;1571
+ ld a,b ;1574
+ pop hl ;1575
+ pop bc ;1576
+ push hl ;1577
+ ex de,hl ;1578
+l1579h:
+ push af ;1579
+ ld a,(hl) ;157a
+ comst ;157b
+ ld (de),a ;157f
+ comend
+ pop af ;1580
+ inc de ;1581
+ cpi ;1582
+ jp po,l1592h ;1584
+ dec a ;1587
+ jr nz,l1579h ;1588
+ pop hl ;158a
+ comst ;158b
+ ldir ;158f
+ comend
+ ret ;1591
+l1592h:
+ pop hl ;1592
+ ret ;1593
+
+sub_1594h:
+ push hl ;1594
+ push de ;1595
+ push bc ;1596
+l1597h:
+ ld a,(de) ;1597
+ comst ;1598
+ cp (hl) ;159c
+ comend
+ jr nz,l15a3h ;159d
+ inc de ;159f
+ inc hl ;15a0
+ djnz l1597h ;15a1
+l15a3h:
+ pop bc ;15a3
+ pop de ;15a4
+ pop hl ;15a5
+ ret ;15a6
+
+sub_15a7h:
+ ld hl,ci.buf+1 ;15a7
+ call get_bytes ;15aa
+ ld de,ci.buf+1 ;15ad
+ and a ;15b0
+ sbc hl,de ;15b1
+ ld b,l ;15b3
+ ret nz ;15b4
+ jp ERROR ;15b5
+
+get_bytes:
+ db 0e6h ;15b8 and 037h (clear carry, skip next opc)
+get_bytes_m:
+ scf
+l15bah:
+ push af ;15ba
+ call skip_to_nextarg ;15bb
+ cp 'W' ;15be
+ jr nz,l15d9h ;15c0
+ inc de ;15c2
+ push hl ;15c3
+ call sub_0a68h ;15c4
+ ex de,hl ;15c7
+ pop bc ;15c8
+ pop af ;15c9
+ push af ;15ca
+ push bc ;15cb
+ ex (sp),hl ;15cc
+ jr nc,l15d3h ;15cd
+ comst ;15cf
+l15d3h:
+ ld (hl),e ;15d3
+ comend
+ inc hl ;15d4
+ ld c,d ;15d5
+ pop de ;15d6
+ jr l15e5h ;15d7
+l15d9h:
+ cp '''' ;15d9
+ jr z,l15f1h ;15db
+ push hl ;15dd
+ call EXPR ;15de
+ ld c,l ;15e1
+ pop hl ;15e2
+ jr c,l1626h ;15e3
+l15e5h:
+ pop af ;15e5
+ push af ;15e6
+ jr nc,l15edh ;15e7
+ comst ;15e9
+l15edh:
+ ld (hl),c ;15ed
+ comend
+ inc hl ;15ee
+ jr l161eh ;15ef
+l15f1h:
+ inc de ;15f1
+ ld a,(de) ;15f2
+ cp '''' ;15f3
+ jr z,l1607h ;15f5
+ or a ;15f7
+ jr z,l1626h ;15f8
+l15fah:
+ ld c,a ;15fa
+ pop af ;15fb
+ push af ;15fc
+ jr nc,l1603h ;15fd
+ comst ;15ff
+l1603h:
+ ld (hl),c ;1603
+ comend
+ inc hl ;1604
+ jr l15f1h ;1605
+l1607h:
+ inc de ;1607
+ ld a,(de) ;1608
+ cp '''' ;1609
+ jr z,l15fah ;160b
+ cp '.' ;160d
+ jr nz,l161eh ;160f
+ inc de ;1611
+ dec hl ;1612
+ pop af ;1613
+ push af ;1614
+ jr nc,l161bh ;1615
+ comst ;1617
+l161bh:
+ set 7,(hl) ;161b
+ comend
+ inc hl ;161d
+l161eh:
+ pop af ;161e
+ jr nc,l15bah ;161f
+ ld (lst.S),hl ;1621
+ jr l15bah ;1624
+
+l1626h:
+ pop af ;1626
+ ret nc ;1627
+ ld (lst.S),hl ;1628
+ ret ;162b
+
+CMD.D:
+ ld hl,CMD.D ;162c
+ ld (CMD_RPT),hl ;162f
+ ld hl,(lst.D) ;1632
+ ld bc,00080h ;1635
+ call sub_0a82h ;1638
+ scf ;163b
+memdump:
+ push bc ;163c
+ push de ;163d
+ push hl ;163e
+ ex af,af' ;163f
+l1640h:
+ call out.hl.@ ;1640
+ call z,outbl2 ;1643
+ call OUTBL ;1646
+ ld de,0 ;1649
+l164ch:
+ comst ;164c
+ ld a,(hl) ;1650
+ comend
+ inc hl ;1651
+ call out.hex ;1652
+ call OUTBL ;1655
+ dec bc ;1658
+ inc e ;1659
+ ld a,e ;165a
+ cp 010h ;165b
+ jr z,l1668h ;165d
+ and 003h ;165f
+ call z,OUTBL ;1661
+ ld a,b ;1664
+ or c ;1665
+ jr nz,l164ch ;1666
+l1668h:
+ call OUTBL ;1668
+ and a ;166b
+ sbc hl,de ;166c
+l166eh:
+ comst ;166e
+ ld a,(hl) ;1672
+ comend
+ call sub_168fh ;1673
+ call OUTCHAR ;1676
+ inc hl ;1679
+ dec e ;167a
+ jr nz,l166eh ;167b
+ ex af,af' ;167d
+ jr nc,l1683h ;167e
+ ld (lst.D),hl ;1680
+l1683h:
+ ex af,af' ;1683
+ call CRLF ;1684
+ ld a,b ;1687
+ or c ;1688
+ jr nz,l1640h ;1689
+ pop hl ;168b
+ pop de ;168c
+ pop bc ;168d
+ ret ;168e
+sub_168fh:
+ and 07fh ;168f
+ cp 07fh ;1691
+ jr z,l1698h ;1693
+ cp 020h ;1695
+ ret nc ;1697
+l1698h:
+ ld a,02eh ;1698
+ ret ;169a
+
+; Read Intel Hex File from console.
+
+CMD.R:
+ ld hl,0 ;169b
+ call get_lastarg_def ;169e
+ push hl ;16a1
+ ld hl,0 ;16a2
+ ld (HILOD),hl ;16a5
+w_recstart:
+ call i.getchar ;16a8
+ jr z,l16deh ;16ab
+ cp ':' ;16ad
+ jr nz,w_recstart ;16af
+ ld c,0 ;16b1 init checksum
+ call i.gethexbyte ;16b3 record len
+ ld b,a ;16b6
+ call i.gethexbyte ;16b7 address high
+ ld h,a ;16ba
+ call i.gethexbyte ;16bb address low
+ ld l,a ;16be
+ call i.gethexbyte ;16bf record type (ignored)
+ ld a,b ;16c2
+ and a ;16c3 record len == 0?
+ jr z,l16deh ;16c4
+l16c6h:
+ call i.gethexbyte ;16c6
+ pop de ;16c9
+ push de ;16ca
+ push hl ;16cb
+ add hl,de ;16cc
+ call i.storebyte ;16cd
+ pop hl ;16d0
+ inc hl ;16d1
+ djnz l16c6h ;16d2
+ call i.gethexbyte ;16d4
+ ld a,c ;16d7
+ and a ;16d8
+ jp nz,ERROR ;16d9 exit if checksum error
+ jr w_recstart ;16dc next record
+l16deh:
+ pop hl ;16de
+ call i.gethexbyte ;16df
+ jp l173fh ;16e2
+
+i.gethexbyte:
+ call sub_16f6h ;16e5
+ rlca ;16e8
+ rlca ;16e9
+ rlca ;16ea
+ rlca ;16eb
+ ld d,a ;16ec
+ call sub_16f6h ;16ed
+ add a,d ;16f0
+ ld d,a ;16f1
+ add a,c ;16f2
+ ld c,a ;16f3
+ ld a,d ;16f4
+ ret ;16f5
+
+sub_16f6h:
+ call i.getchar ;16f6
+ jr z,l16ffh ;16f9
+ call sub_0d26h ;16fb
+ ret nc ;16fe
+l16ffh:
+ jp ERROR ;16ff
+
+i.getchar:
+ call $ci ;1702
+ cp 01ah ;1705
+ ret ;1707
+
+i.storebyte:
+ push af ;1708
+ push de ;1709
+ ld de,TPA ;170a
+ call CP.HL.DE ;170d
+ jp c,ERROR ;1710
+ ld de,$stcka ;1713
+ call CP.HL.DE ;1716
+ jp nc,ERROR ;1719
+ ld de,(HILOD) ;171c
+ call CP.HL.DE ;1720
+ jr c,l1728h ;1723
+ ld (HILOD),hl ;1725
+l1728h:
+ ld de,(MAXLOD) ;1728
+ call CP.HL.DE ;172c
+ jr c,l1734h ;172f
+ ld (MAXLOD),hl ;1731
+l1734h:
+ pop de ;1734
+ pop af ;1735
+ comst ;1736
+ ld (hl),a ;173a
+ comend
+ ret ;173b
+
+l173ch:
+ call assert_eol ;173c
+l173fh:
+ ld hl,MSG_high ;173f
+ call PSTR ;1742
+ ld hl,(HILOD) ;1745
+ call out.hl ;1748
+ ld hl,MSG_max ;174b
+ call PSTR ;174e
+ ld hl,(MAXLOD) ;1751
+ call out.hl ;1754
+ jp CRLF ;1757
+
+MSG_high:
+ DC 'High = '
+MSG_max:
+ DC ' Max = '
+
+CMD.A:
+ ld hl,(lst.A) ;1769
+ call get_lastarg_def ;176c
+ push hl ;176f
+ pop iy ;1770
+ ld hl,l17c4h ;1772
+ ld (CMD_ERR),hl ;1775
+ ld (XB068),sp ;1778
+l177ch:
+ push iy ;177c
+ pop hl ;177e
+ ld (lst.A),hl ;177f
+ ld (OFFS.pc),hl ;1782
+ push hl ;1785
+ call sub_1f3fh ;1786
+ pop iy ;1789
+ ld c,b ;178b
+ ld de,(offs.@) ;178c
+ ld a,d ;1790
+ or e ;1791
+ ld b,011h ;1792
+ jr z,l1798h ;1794
+ ld b,019h ;1796
+l1798h:
+ call OUTBL ;1798
+ ld a,(CON.COL) ;179b
+ cp b ;179e
+ jr c,l1798h ;179f
+ push bc ;17a1
+ call INLINE ;17a2
+ pop bc ;17a5
+ call SKIPBL ;17a6
+ cp '.' ;17a9
+ ret z ;17ab
+ cp '-' ;17ac
+ jr nz,l17b6h ;17ae
+ ld iy,(XB06C) ;17b0
+ jr l177ch ;17b4
+l17b6h:
+ and a ;17b6
+ call nz,sub_17cdh ;17b7
+ ld (XB06C),iy ;17ba
+ ld b,0 ;17be
+ add iy,bc ;17c0
+ jr l177ch ;17c2
+l17c4h:
+ call l07eah ;17c4
+ ld sp,(XB068) ;17c7
+ jr l177ch ;17cb
+
+sub_17cdh:
+ call SKIPBL ;17cd
+ ld hl,t_MNEMONICS ;17d0
+ call sub_0a15h ;17d3
+ jp nc,ERROR ;17d6
+ call SKIPBL ;17d9
+ push de ;17dc
+ ld a,b ;17dd
+ add a,b ;17de
+ add a,b ;17df
+ ld hl,b_0x17EE_start ;17e0
+ call ADD_HL_A ;17e3
+ ld e,(hl) ;17e6
+ inc hl ;17e7
+ ld d,(hl) ;17e8
+ inc hl ;17e9
+ ld b,(hl) ;17ea
+ ex de,hl ;17eb
+ pop de ;17ec
+ jp (hl) ;17ed
+
+b_0x17EE_start:
+ defw l1b54h ;17ee
+
+b_0x17F0_start:
+ defb 088h ;17f0
+
+b_0x17F1_start:
+ defw l1b74h ;17f1
+
+b_0x17F3_start:
+ defb 080h ;17f3
+
+b_0x17F4_start:
+ defw l1babh ;17f4
+
+b_0x17F6_start:
+ defb 0a0h ;17f6
+
+b_0x17F7_start:
+ defw l1c14h ;17f7
+
+b_0x17F9_start:
+ defb 040h ;17f9
+
+b_0x17FA_start:
+ defw l1c38h ;17fa
+
+b_0x17FC_start:
+ defb 0c4h ;17fc
+
+b_0x17FD_start:
+ defw l1b36h ;17fd
+
+b_0x17FF_start:
+ defb 03fh ;17ff
+
+b_0x1800_start:
+ defw l1babh ;1800
+
+b_0x1802_start:
+ defb 0b8h ;1802
+
+b_0x1803_start:
+ defw gen.opc.ED2 ;1803
+
+b_0x1805_start:
+ defb 0a9h ;1805
+
+b_0x1806_start:
+ defw gen.opc.ED2 ;1806
+
+b_0x1808_start:
+ defb 0b9h ;1808
+
+b_0x1809_start:
+ defw gen.opc.ED2 ;1809
+
+b_0x180B_start:
+ defb 0a1h ;180b
+
+b_0x180C_start:
+ defw gen.opc.ED2 ;180c
+
+b_0x180E_start:
+ defb 0b1h ;180e
+
+b_0x180F_start:
+ defw l1b36h ;180f
+
+b_0x1811_start:
+ defb 02fh ;1811
+
+b_0x1812_start:
+ defw l1b36h ;1812
+
+b_0x1814_start:
+ defb 027h ;1814
+
+b_0x1815_start:
+ defw l1dabh ;1815
+
+b_0x1817_start:
+ defb 005h ;1817
+
+b_0x1818_start:
+ defw l1b36h ;1818
+
+b_0x181A_start:
+ defb 0f3h ;181a
+
+b_0x181B_start:
+ defw l1ca4h ;181b
+
+b_0x181D_start:
+ defb 010h ;181d
+
+b_0x181E_start:
+ defw l1b36h ;181e
+
+b_0x1820_start:
+ defb 0fbh ;1820
+
+b_0x1821_start:
+ defw l1d54h ;1821
+
+b_0x1823_start:
+ defb 0e3h ;1823
+
+b_0x1824_start:
+ defw l1b36h ;1824
+
+b_0x1826_start:
+ defb 0d9h ;1826
+
+b_0x1827_start:
+ defw l1b36h ;1827
+
+b_0x1829_start:
+ defb 076h ;1829
+
+b_0x182A_start:
+ defw l1cbfh ;182a
+
+b_0x182C_start:
+ defb 046h ;182c
+
+b_0x182D_start:
+ defw l1cf8h ;182d
+
+b_0x182F_start:
+ defb 040h ;182f
+
+b_0x1830_start:
+ defw l1dabh ;1830
+
+b_0x1832_start:
+ defb 004h ;1832
+
+b_0x1833_start:
+ defw gen.opc.ED2 ;1833
+
+b_0x1835_start:
+ defb 0aah ;1835
+
+b_0x1836_start:
+ defw gen.opc.ED2 ;1836
+
+b_0x1838_start:
+ defb 0bah ;1838
+
+b_0x1839_start:
+ defw gen.opc.ED2 ;1839
+
+b_0x183B_start:
+ defb 0a2h ;183b
+
+b_0x183C_start:
+ defw gen.opc.ED2 ;183c
+
+b_0x183E_start:
+ defb 0b2h ;183e
+
+b_0x183F_start:
+ defw l1c5eh ;183f
+
+b_0x1841_start:
+ defb 0c2h ;1841
+
+b_0x1842_start:
+ defw l1cabh ;1842
+
+b_0x1844_start:
+ defb 020h ;1844
+
+b_0x1845_start:
+ defw l1934h ;1845
+
+b_0x1847_start:
+ defb 040h ;1847
+
+b_0x1848_start:
+ defw gen.opc.ED2 ;1848
+
+b_0x184A_start:
+ defb 0a8h ;184a
+
+b_0x184B_start:
+ defw gen.opc.ED2 ;184b
+
+b_0x184D_start:
+ defb 0b8h ;184d
+
+b_0x184E_start:
+ defw gen.opc.ED2 ;184e
+
+b_0x1850_start:
+ defb 0a0h ;1850
+
+b_0x1851_start:
+ defw gen.opc.ED2 ;1851
+
+b_0x1853_start:
+ defb 0b0h ;1853
+
+b_0x1854_start:
+ defw gen.opc.ED2 ;1854
+
+b_0x1856_start:
+ defb 044h ;1856
+
+b_0x1857_start:
+ defw l1b36h ;1857
+
+b_0x1859_start:
+ defb 000h ;1859
+
+b_0x185A_start:
+ defw l1babh ;185a
+
+b_0x185C_start:
+ defb 0b0h ;185c
+
+b_0x185D_start:
+ defw gen.opc.ED2 ;185d
+
+b_0x185F_start:
+ defb 0bbh ;185f
+
+b_0x1860_start:
+ defw gen.opc.ED2 ;1860
+
+b_0x1862_start:
+ defb 0b3h ;1862
+
+b_0x1863_start:
+ defw l1d2ch ;1863
+
+b_0x1865_start:
+ defb 041h ;1865
+
+b_0x1866_start:
+ defw gen.opc.ED2 ;1866
+
+b_0x1868_start:
+ defb 0abh ;1868
+
+b_0x1869_start:
+ defw gen.opc.ED2 ;1869
+
+b_0x186B_start:
+ defb 0a3h ;186b
+
+b_0x186C_start:
+ defw l1ce4h ;186c
+
+b_0x186E_start:
+ defb 0c1h ;186e
+
+b_0x186F_start:
+ defw l1ce4h ;186f
+
+b_0x1871_start:
+ defb 0c5h ;1871
+
+b_0x1872_start:
+ defw l1c14h ;1872
+
+b_0x1874_start:
+ defb 080h ;1874
+
+b_0x1875_start:
+ defw l1c50h ;1875
+
+b_0x1877_start:
+ defb 0c0h ;1877
+
+b_0x1878_start:
+ defw gen.opc.ED2 ;1878
+
+b_0x187A_start:
+ defb 04dh ;187a
+
+b_0x187B_start:
+ defw gen.opc.ED2 ;187b
+
+b_0x187D_start:
+ defb 045h ;187d
+
+b_0x187E_start:
+ defw l1bd8h ;187e
+
+b_0x1880_start:
+ defb 010h ;1880
+
+b_0x1881_start:
+ defw l1b36h ;1881
+
+b_0x1883_start:
+ defb 017h ;1883
+
+b_0x1884_start:
+ defw l1bd8h ;1884
+
+b_0x1886_start:
+ defb 000h ;1886
+
+b_0x1887_start:
+ defw l1b36h ;1887
+
+b_0x1889_start:
+ defb 007h ;1889
+
+b_0x188A_start:
+ defw gen.opc.ED2 ;188a
+
+b_0x188C_start:
+ defb 06fh ;188c
+
+b_0x188D_start:
+ defw l1bd8h ;188d
+
+b_0x188F_start:
+ defb 018h ;188f
+
+b_0x1890_start:
+ defw l1b36h ;1890
+
+b_0x1892_start:
+ defb 01fh ;1892
+
+b_0x1893_start:
+ defw l1bd8h ;1893
+
+b_0x1895_start:
+ defb 008h ;1895
+
+b_0x1896_start:
+ defw l1b36h ;1896
+
+b_0x1898_start:
+ defb 00fh ;1898
+
+b_0x1899_start:
+ defw gen.opc.ED2 ;1899
+
+b_0x189B_start:
+ defb 067h ;189b
+
+b_0x189C_start:
+ defw l1cd5h ;189c
+
+b_0x189E_start:
+ defb 0c7h ;189e
+
+b_0x189F_start:
+ defw l1b54h ;189f
+
+b_0x18A1_start:
+ defb 098h ;18a1
+
+b_0x18A2_start:
+ defw l1b36h ;18a2
+
+b_0x18A4_start:
+ defb 037h ;18a4
+
+b_0x18A5_start:
+ defw l1c14h ;18a5
+
+b_0x18A7_start:
+ defb 0c0h ;18a7
+
+b_0x18A8_start:
+ defw l1bd8h ;18a8
+
+b_0x18AA_start:
+ defb 020h ;18aa
+
+b_0x18AB_start:
+ defw l1bd8h ;18ab
+
+b_0x18AD_start:
+ defb 028h ;18ad
+
+b_0x18AE_start:
+ defw l1bd8h ;18ae
+
+b_0x18B0_start:
+ defb 038h ;18b0
+
+b_0x18B1_start:
+ defw l1babh ;18b1
+
+b_0x18B3_start:
+ defb 090h ;18b3
+
+b_0x18B4_start:
+ defw l1babh ;18b4
+
+b_0x18B6_start:
+ defb 0a8h ;18b6
+
+b_0x18B7_start:
+ defw A.IN0 ;18b7
+
+b_0x18B9_start:
+ defb 000h ;18b9
+
+b_0x18BA_start:
+ defw A.MLT ;18ba
+
+b_0x18BC_start:
+ defb 04ch ;18bc
+ ld b,e ;18bd
+ dec de ;18be
+
+b_0x18BF_start:
+ defb 08bh ;18bf
+
+b_0x18C0_start:
+ defw gen.opc.ED2 ;18c0
+
+b_0x18C2_start:
+ defb 09bh ;18c2
+
+b_0x18C3_start:
+ defw gen.opc.ED2 ;18c3
+
+b_0x18C5_start:
+ defb 083h ;18c5
+
+b_0x18C6_start:
+ defw gen.opc.ED2 ;18c6
+
+b_0x18C8_start:
+ defb 093h ;18c8
+
+b_0x18C9_start:
+ defw l18fdh ;18c9
+
+b_0x18CB_start:
+ defb 001h ;18cb
+
+b_0x18CC_start:
+ defw gen.opc.ED2 ;18cc
+
+b_0x18CE_start:
+ defb 076h ;18ce
+
+b_0x18CF_start:
+ defw l191dh ;18cf
+
+b_0x18D1_start:
+ defb 004h ;18d1
+
+b_0x18D2_start:
+ defw l192ch ;18d2
+
+b_0x18D4_start:
+ defb 074h ;18d4
+A.IN0:
+ call sub_1e41h ;18d5
+ jr nc,l1931h ;18d8
+ cp 006h ;18da
+ jr z,l1931h ;18dc
+ rlca ;18de
+ rlca ;18df
+ rlca ;18e0
+ add a,b ;18e1
+ ld b,a ;18e2
+ call sub_1ed1h ;18e3
+ call sub_1e06h ;18e6
+l18e9h:
+ call assert_eol ;18e9
+ comst ;18ec
+ ld (iy+000h),0edh ;18f0
+ ld (iy+001h),b ;18f4
+ ld (iy+002h),l ;18f7
+ comend
+ ld c,003h ;18fa
+ ret ;18fc
+l18fdh:
+ call sub_1e06h ;18fd
+ call sub_1ed1h ;1900
+ call sub_1e41h ;1903
+ jr nc,l1931h ;1906
+ cp 006h ;1908
+ jr z,l1931h ;190a
+ rlca ;190c
+ rlca ;190d
+ rlca ;190e
+ add a,b ;190f
+ ld b,a ;1910
+ jr l18e9h ;1911
+A.MLT:
+ call sub_1e2eh ;1913
+ jr nc,l1931h ;1916
+ add a,b ;1918
+ ld b,a ;1919
+ jp gen.opc.ED2 ;191a
+l191dh:
+ call sub_1e41h ;191d
+ jr nc,l192ah ;1920
+ rlca ;1922
+ rlca ;1923
+ rlca ;1924
+ add a,b ;1925
+ ld b,a ;1926
+ jp gen.opc.ED2 ;1927
+l192ah:
+ ld b,064h ;192a
+l192ch:
+ call sub_1e12h ;192c
+ jr l18e9h ;192f
+l1931h:
+ jp ERROR ;1931
+l1934h:
+ call sub_1e41h ;1934
+ jp c,l19bfh ;1937
+ call sub_1e68h ;193a
+ jp c,l1a64h ;193d
+ call sub_1e2eh ;1940
+ jp c,l1a93h ;1943
+ call sub_1e50h ;1946
+ jp c,l1af0h ;1949
+ ld a,(de) ;194c
+ cp 049h ;194d
+ jp z,l1b0ch ;194f
+ cp 052h ;1952
+ jp z,l1b14h ;1954
+ cp 028h ;1957
+ jp nz,ERROR ;1959
+ inc de ;195c
+ call sub_1e2eh ;195d
+ jp c,l1b23h ;1960
+ call tst_EXPR ;1963
+ call sub_1ed8h ;1966
+ call sub_1ed1h ;1969
+ call sub_1e2eh ;196c
+ jr c,l19adh ;196f
+ call sub_1e50h ;1971
+ jr nc,l1991h ;1974
+ ld b,022h ;1976
+l1978h:
+ call assert_eol ;1978
+ ld a,(pfx.IXY) ;197b
+l197eh:
+ comst ;197e
+ ld (iy+000h),a ;1982
+ ld (iy+001h),b ;1985
+ ld (iy+002h),l ;1988
+ ld (iy+003h),h ;198b
+ comend
+ ld c,004h ;198e
+ ret ;1990
+l1991h:
+ ld a,(de) ;1991
+ cp 041h ;1992
+ jp nz,ERROR ;1994
+ inc de ;1997
+ ld b,032h ;1998
+l199ah:
+ call assert_eol ;199a
+ comst ;199d
+ ld (iy+000h),b ;19a1
+ ld (iy+001h),l ;19a4
+ ld (iy+002h),h ;19a7
+ comend
+ ld c,003h ;19aa
+ ret ;19ac
+l19adh:
+ cp 020h ;19ad
+ jr z,l19bbh ;19af
+ add a,043h ;19b1
+ ld b,a ;19b3
+l19b4h:
+ call assert_eol ;19b4
+ ld a,0edh ;19b7
+ jr l197eh ;19b9
+l19bbh:
+ ld b,022h ;19bb
+ jr l199ah ;19bd
+l19bfh:
+ ld b,a ;19bf
+ call sub_1ed1h ;19c0
+ call sub_1e41h ;19c3
+ jr nc,l19dbh ;19c6
+ push af ;19c8
+ ld a,b ;19c9
+ rlca ;19ca
+ rlca ;19cb
+ rlca ;19cc
+ ld b,a ;19cd
+ pop af ;19ce
+ add a,b ;19cf
+ add a,040h ;19d0
+ cp 076h ;19d2
+ jp z,ERROR ;19d4
+l19d7h:
+ ld b,a ;19d7
+ jp l1b36h ;19d8
+l19dbh:
+ call sub_1e68h ;19db
+ jr nc,l1a02h ;19de
+ ld a,b ;19e0
+ rlca ;19e1
+ rlca ;19e2
+ rlca ;19e3
+ add a,046h ;19e4
+ cp 076h ;19e6
+ jp z,ERROR ;19e8
+l19ebh:
+ ld b,a ;19eb
+ call assert_eol ;19ec
+ ld a,(pfx.IXY) ;19ef
+ comst ;19f2
+ ld (iy+000h),a ;19f6
+ ld (iy+001h),b ;19f9
+ ld (iy+002h),c ;19fc
+ comend
+ ld c,003h ;19ff
+ ret ;1a01
+l1a02h:
+ ld a,(de) ;1a02
+ cp 'I' ;1a03
+ jr z,l1a15h ;1a05
+ cp 'R' ;1a07
+ jr nz,l1a21h ;1a09
+ ld a,b ;1a0b
+ cp 007h ;1a0c
+ jp nz,ERROR ;1a0e
+ ld b,05fh ;1a11
+ jr l1a1dh ;1a13
+l1a15h:
+ ld a,b ;1a15
+ cp 007h ;1a16
+ jp nz,ERROR ;1a18
+ ld b,057h ;1a1b
+l1a1dh:
+ inc de ;1a1d
+ jp gen.opc.ED2 ;1a1e
+l1a21h:
+ cp '(' ;1a21
+ jr z,l1a3fh ;1a23
+ call sub_1e12h ;1a25
+ ld a,b ;1a28 ld r,nn
+ rlca ;1a29
+ rlca ;1a2a
+ rlca ;1a2b
+ add a,006h ;1a2c
+l1a2eh:
+ ld b,a ;1a2e
+l1a2fh:
+ call assert_eol ;1a2f
+ comst ;1a32
+ ld (iy+000h),b ;1a36
+ ld (iy+001h),l ;1a39
+ comend
+ ld c,002h ;1a3c
+ ret ;1a3e
+l1a3fh:
+ inc de ;1a3f
+ ld a,b ;1a40
+ cp 007h ;1a41
+ jp nz,ERROR ;1a43
+ call sub_1e2eh ;1a46
+ jr nc,l1a59h ;1a49
+ cp 030h ;1a4b
+ jp nc,ERROR ;1a4d
+ add a,00ah ;1a50
+ ld b,a ;1a52
+ call sub_1ed8h ;1a53
+ jp l1b36h ;1a56
+l1a59h:
+ call tst_EXPR ;1a59
+ call sub_1ed8h ;1a5c
+ ld b,03ah ;1a5f
+ jp l199ah ;1a61
+l1a64h:
+ call sub_1ed1h ;1a64
+ call sub_1e41h ;1a67
+ jr nc,l1a76h ;1a6a
+ cp 006h ;1a6c
+ jp z,ERROR ;1a6e
+ add a,070h ;1a71
+ jp l19ebh ;1a73
+l1a76h:
+ call sub_1e12h ;1a76
+ call assert_eol ;1a79
+ ld a,(pfx.IXY) ;1a7c
+ comst ;1a7f
+ ld (iy+000h),a ;1a83 dd/fd
+ ld (iy+001h),036h ;1a86
+ ld (iy+002h),c ;1a8a displacement
+ ld (iy+003h),l ;1a8d nn
+ comend
+ ld c,4 ;1a90
+ ret ;1a92
+l1a93h:
+ ld b,a ;1a93
+ call sub_1ed1h ;1a94
+ ld hl,t_HL.AF ;1a97
+ call sub_0a23h ;1a9a
+ jr c,l1abeh ;1a9d
+ call sub_1e50h ;1a9f
+ jr nc,l1ac7h ;1aa2
+ ld a,b ;1aa4
+ cp 030h ;1aa5
+ jr nz,l1b20h ;1aa7
+ ld b,0f9h ;1aa9
+l1aabh:
+ call assert_eol ;1aab
+ ld a,(pfx.IXY) ;1aae
+ comst ;1ab1
+ ld (iy+000h),a ;1ab5
+ ld (iy+001h),b ;1ab8
+ comend
+ ld c,002h ;1abb
+ ret ;1abd
+l1abeh:
+ ld a,b ;1abe
+ cp 030h ;1abf
+ jr nz,l1b20h ;1ac1
+ ld b,0f9h ;1ac3
+ jr l1b36h ;1ac5
+l1ac7h:
+ ld a,(de) ;1ac7
+ cp 028h ;1ac8
+ jr nz,l1ae3h ;1aca
+ inc de ;1acc
+ call tst_EXPR ;1acd
+ call sub_1ed8h ;1ad0
+ ld a,b ;1ad3
+ cp 020h ;1ad4
+ jr z,l1adeh ;1ad6
+ add a,04bh ;1ad8
+ ld b,a ;1ada
+ jp l19b4h ;1adb
+l1adeh:
+ ld b,02ah ;1ade
+ jp l199ah ;1ae0
+l1ae3h:
+ call tst_EXPR ;1ae3
+ call assert_eol ;1ae6
+ ld a,001h ;1ae9
+ add a,b ;1aeb
+ ld b,a ;1aec
+ jp l199ah ;1aed
+l1af0h:
+ call sub_1ed1h ;1af0
+ ld a,(de) ;1af3
+ cp 028h ;1af4
+ jr nz,l1b04h ;1af6
+ inc de ;1af8
+ call tst_EXPR ;1af9
+ call sub_1ed8h ;1afc
+ ld b,02ah ;1aff
+ jp l1978h ;1b01
+l1b04h:
+ call tst_EXPR ;1b04
+ ld b,021h ;1b07
+ jp l1978h ;1b09
+l1b0ch:
+ inc de ;1b0c
+ call sub_1ed1h ;1b0d
+ ld b,047h ;1b10
+ jr l1b1ah ;1b12
+l1b14h:
+ inc de ;1b14
+ call sub_1ed1h ;1b15
+ ld b,04fh ;1b18
+l1b1ah:
+ ld a,(de) ;1b1a
+ inc de ;1b1b
+ cp 041h ;1b1c
+ jr z,gen.opc.ED2 ;1b1e
+l1b20h:
+ jp ERROR ;1b20
+l1b23h:
+ cp 020h ;1b23
+ jr nc,l1b20h ;1b25
+ add a,002h ;1b27
+ ld b,a ;1b29
+ call sub_1ed8h ;1b2a
+ call sub_1ed1h ;1b2d
+ ld a,(de) ;1b30
+ cp 041h ;1b31
+ jr nz,l1b20h ;1b33
+ inc de ;1b35
+l1b36h:
+ call assert_eol ;1b36
+ comst ;1b39
+ ld (iy+000h),b ;1b3d
+ comend
+ ld c,001h ;1b40
+ ret ;1b42
+gen.opc.ED2:
+ call assert_eol ;1b43
+ comst ;1b46
+ ld (iy+000h),0edh ;1b4a
+ ld (iy+001h),b ;1b4e
+ comend
+ ld c,002h ;1b51
+ ret ;1b53
+l1b54h:
+ ld hl,t_HL.AF ;1b54
+ call sub_0a23h ;1b57
+ jr nc,l1babh ;1b5a
+ call sub_1ed1h ;1b5c
+ call sub_1e2eh ;1b5f
+ jp nc,ERROR ;1b62
+ push af ;1b65
+ ld a,b ;1b66
+ cp 088h ;1b67
+ ld b,04ah ;1b69
+ jr z,l1b6fh ;1b6b
+ ld b,042h ;1b6d
+l1b6fh:
+ pop af ;1b6f
+ add a,b ;1b70
+l1b71h:
+ ld b,a ;1b71
+ jr gen.opc.ED2 ;1b72
+l1b74h:
+ ld hl,t_HL.AF ;1b74
+ call sub_0a23h ;1b77
+ jr c,l1b9dh ;1b7a
+ call sub_1e50h ;1b7c
+ jr nc,l1babh ;1b7f
+ call sub_1ed1h ;1b81
+ ld hl,t_BC.DE.IX.SP ;1b84
+ ld a,(pfx.IXY) ;1b87
+ cp 0fdh ;1b8a
+ jr nz,l1b91h ;1b8c
+ ld hl,t_BC.DE.IY.SP ;1b8e
+l1b91h:
+ call sub_1e2bh ;1b91
+ jp nc,ERROR ;1b94
+ add a,009h ;1b97
+l1b99h:
+ ld b,a ;1b99
+ jp l1aabh ;1b9a
+l1b9dh:
+ call sub_1ed1h ;1b9d
+ call sub_1e2eh ;1ba0
+ jp nc,ERROR ;1ba3
+ add a,009h ;1ba6
+ jp l19d7h ;1ba8
+l1babh:
+ ld a,(de) ;1bab
+ cp 041h ;1bac
+ jr nz,l1bbbh ;1bae
+ push de ;1bb0
+ inc de ;1bb1
+ call skip_to_nextarg ;1bb2
+ jr z,l1bbah ;1bb5
+ pop de ;1bb7
+ jr l1bbbh ;1bb8
+l1bbah:
+ pop af ;1bba
+l1bbbh:
+ call sub_1e41h ;1bbb
+ jr c,l1bceh ;1bbe
+ call sub_1e68h ;1bc0
+ jr c,l1bd2h ;1bc3
+ call sub_1e12h ;1bc5
+ ld a,b ;1bc8
+ add a,046h ;1bc9
+ jp l1a2eh ;1bcb
+l1bceh:
+ add a,b ;1bce
+ jp l19d7h ;1bcf
+l1bd2h:
+ ld a,b ;1bd2
+ add a,006h ;1bd3
+ jp l19ebh ;1bd5
+l1bd8h:
+ call sub_1e41h ;1bd8
+ jr c,l1c01h ;1bdb
+ call sub_1e68h ;1bdd
+ jp nc,ERROR ;1be0
+ ld a,b ;1be3
+ add a,006h ;1be4
+ ld b,a ;1be6
+l1be7h:
+ call assert_eol ;1be7
+ ld a,(pfx.IXY) ;1bea
+ comst ;1bed
+ ld (iy+000h),a ;1bf1
+ ld (iy+001h),0cbh ;1bf4
+ ld (iy+002h),c ;1bf8
+ ld (iy+003h),b ;1bfb
+ comend
+ ld c,004h ;1bfe
+ ret ;1c00
+l1c01h:
+ add a,b ;1c01
+l1c02h:
+ ld b,a ;1c02
+ call assert_eol ;1c03
+ comst ;1c06
+ ld (iy+000h),0cbh ;1c0a
+ ld (iy+001h),b ;1c0e
+ comend
+ ld c,002h ;1c11
+ ret ;1c13
+l1c14h:
+ call sub_1de6h ;1c14
+ call sub_1ed1h ;1c17
+ call sub_1e41h ;1c1a
+ jr c,l1c2fh ;1c1d
+ call sub_1e68h ;1c1f
+ jp nc,ERROR ;1c22
+ ld a,l ;1c25
+ rlca ;1c26
+ rlca ;1c27
+ rlca ;1c28
+ add a,006h ;1c29
+ add a,b ;1c2b
+ ld b,a ;1c2c
+ jr l1be7h ;1c2d
+l1c2fh:
+ add a,b ;1c2f
+ ld b,a ;1c30
+ ld a,l ;1c31
+ rlca ;1c32
+ rlca ;1c33
+ rlca ;1c34
+ add a,b ;1c35
+ jr l1c02h ;1c36
+l1c38h:
+ push de ;1c38
+ call sub_1eb8h ;1c39
+ jr nc,l1c47h ;1c3c
+ add a,b ;1c3e
+ ld b,a ;1c3f
+ call skip_to_nextarg ;1c40
+ jr z,l1c49h ;1c43
+ pop de ;1c45
+ push de ;1c46
+l1c47h:
+ ld b,0cdh ;1c47
+l1c49h:
+ pop af ;1c49
+ call tst_EXPR ;1c4a
+ jp l199ah ;1c4d
+l1c50h:
+ call sub_1eb8h ;1c50
+ jr nc,l1c59h ;1c53
+ add a,b ;1c55
+ ld b,a ;1c56
+ jr l1c5bh ;1c57
+l1c59h:
+ ld b,0c9h ;1c59
+l1c5bh:
+ jp l1b36h ;1c5b
+l1c5eh:
+ push de ;1c5e
+ call sub_1eb8h ;1c5f
+ jr c,l1c71h ;1c62
+l1c64h:
+ pop de ;1c64
+ ld hl,b_0x1C97_start ;1c65
+ call sub_0a15h ;1c68
+ jr c,l1c7fh ;1c6b
+ ld b,0c3h ;1c6d
+ jr l1c79h ;1c6f
+l1c71h:
+ add a,b ;1c71
+ ld b,a ;1c72
+ call skip_to_nextarg ;1c73
+ jr nz,l1c64h ;1c76
+ pop af ;1c78
+l1c79h:
+ call tst_EXPR ;1c79
+ jp l199ah ;1c7c
+l1c7fh:
+ call assert_eol ;1c7f
+ ld a,b ;1c82
+ and a ;1c83
+ jr nz,l1c8bh ;1c84
+ ld b,0e9h ;1c86
+ jp l1b36h ;1c88
+l1c8bh:
+ ld b,0ddh ;1c8b
+ dec a ;1c8d
+ jr z,l1c92h ;1c8e
+ ld b,0fdh ;1c90
+l1c92h:
+ ld l,0e9h ;1c92
+ jp l1a2fh ;1c94
+
+b_0x1C97_start:
+ DC '(HL)'
+ DC '(IX)'
+ DC '(IY)'
+ DB 0
+
+l1ca4h:
+ call skip_to_nextarg ;1ca4
+ ld b,010h ;1ca7
+ jr l1cb9h ;1ca9
+l1cabh:
+ call sub_1ebfh ;1cab
+ jr c,l1cb4h ;1cae
+ ld b,018h ;1cb0
+ jr l1cb9h ;1cb2
+l1cb4h:
+ add a,b ;1cb4
+ ld b,a ;1cb5
+ call sub_1ed1h ;1cb6
+l1cb9h:
+ call sub_1defh ;1cb9
+ jp l1a2fh ;1cbc
+l1cbfh:
+ call sub_1e12h ;1cbf
+ ld a,l ;1cc2
+ cp 003h ;1cc3
+ jr nc,l1d23h ;1cc5
+ and a ;1cc7
+ jr z,l1cd2h ;1cc8
+ ld b,056h ;1cca
+ cp 001h ;1ccc
+ jr z,l1cd2h ;1cce
+ ld b,05eh ;1cd0
+l1cd2h:
+ jp gen.opc.ED2 ;1cd2
+l1cd5h:
+ call sub_1e12h ;1cd5
+ ld a,l ;1cd8
+ push af ;1cd9
+ add a,b ;1cda
+ ld b,a ;1cdb
+ pop af ;1cdc
+ and 0c7h ;1cdd
+ jr nz,l1d23h ;1cdf
+ jp l1b36h ;1ce1
+l1ce4h:
+ call sub_1e50h ;1ce4
+ jr c,l1cf2h ;1ce7
+ call sub_1e25h ;1ce9
+ jr nc,l1d23h ;1cec
+ add a,b ;1cee
+ jp l19d7h ;1cef
+l1cf2h:
+ ld a,b ;1cf2
+ add a,020h ;1cf3
+ jp l1b99h ;1cf5
+l1cf8h:
+ call sub_1e41h ;1cf8
+ jr nc,l1d23h ;1cfb
+ cp 006h ;1cfd
+ jr z,l1d23h ;1cff
+ rlca ;1d01
+ rlca ;1d02
+ rlca ;1d03
+ add a,b ;1d04
+ ld b,a ;1d05
+ cp 078h ;1d06
+ jr nz,l1d1ah ;1d08
+ call sub_1ed1h ;1d0a
+ call sub_1d26h ;1d0d
+ jr c,l1d20h ;1d10
+ call sub_1e06h ;1d12
+ ld b,0dbh ;1d15
+ jp l1a2fh ;1d17
+l1d1ah:
+ call sub_1ed1h ;1d1a
+ call sub_1d26h ;1d1d
+l1d20h:
+ jp c,gen.opc.ED2 ;1d20
+l1d23h:
+ jp ERROR ;1d23
+sub_1d26h:
+ ld hl,t__C_ ;1d26
+ jp sub_0a23h ;1d29
+l1d2ch:
+ call sub_1d26h ;1d2c
+ jr nc,l1d44h ;1d2f
+ call sub_1ed1h ;1d31
+ call sub_1e41h ;1d34
+ jr nc,l1d23h ;1d37
+ cp 006h ;1d39
+ jr z,l1d23h ;1d3b
+ rlca ;1d3d
+ rlca ;1d3e
+ rlca ;1d3f
+ add a,b ;1d40
+ jp l1b71h ;1d41
+l1d44h:
+ call sub_1e06h ;1d44
+ call sub_1ed1h ;1d47
+ cp 041h ;1d4a
+ jr nz,l1d23h ;1d4c
+ inc de ;1d4e
+ ld b,0d3h ;1d4f
+ jp l1a2fh ;1d51
+l1d54h:
+ ld hl,b_0x1D80_start ;1d54
+ call sub_0a15h ;1d57
+ jp nc,ERROR ;1d5a
+ ld c,b ;1d5d
+ call assert_eol ;1d5e
+ ld b,000h ;1d61
+ ld hl,b_0x1DA1_start ;1d63
+ add hl,bc ;1d66
+ add hl,bc ;1d67
+ ld a,(hl) ;1d68
+ comst ;1d69
+ ld (iy+000h),a ;1d6d
+ comend
+ ld c,001h ;1d70
+ inc hl ;1d72
+ ld a,(hl) ;1d73
+ and a ;1d74
+ ret z ;1d75
+ comst ;1d76
+ ld (iy+001h),a ;1d7a
+ comend
+ ld c,002h ;1d7d
+ ret ;1d7f
+
+b_0x1D80_start:
+ DC 'AF,AF'''
+l1d86h:
+ DC 'DE,HL'
+ DC '(SP),HL'
+ DC '(SP),IX'
+ DC '(SP),IY'
+ db 000h ;1da0
+
+b_0x1DA1_start:
+ db 008h ;1da1
+ db 000h ;1da2
+ db 0ebh ;1da3
+ db 000h ;1da4
+ db 0e3h ;1da5
+ db 000h ;1da6
+ db 0ddh ;1da7
+ db 0e3h ;1da8
+ db 0fdh ;1da9
+ db 0e3h ;1daa
+l1dabh:
+ call sub_1e50h ;1dab
+ jr c,l1dc6h ;1dae
+ call sub_1e2eh ;1db0
+ jr c,l1dd2h ;1db3
+ call sub_1e41h ;1db5
+ jr c,l1ddfh ;1db8
+ call sub_1e68h ;1dba
+ jp nc,ERROR ;1dbd
+ ld a,b ;1dc0
+ add a,030h ;1dc1
+ jp l19ebh ;1dc3
+l1dc6h:
+ ld a,b ;1dc6
+ ld b,023h ;1dc7
+ cp 004h ;1dc9
+ jr z,l1dcfh ;1dcb
+ ld b,02bh ;1dcd
+l1dcfh:
+ jp l1aabh ;1dcf
+l1dd2h:
+ push af ;1dd2
+ ld a,b ;1dd3
+ ld b,003h ;1dd4
+ cp 004h ;1dd6
+ jr z,l1ddch ;1dd8
+ ld b,00bh ;1dda
+l1ddch:
+ pop af ;1ddc
+ jr l1de2h ;1ddd
+l1ddfh:
+ rlca ;1ddf
+ rlca ;1de0
+ rlca ;1de1
+l1de2h:
+ add a,b ;1de2
+ jp l19d7h ;1de3
+sub_1de6h:
+ call sub_1e12h ;1de6
+ ld a,l ;1de9
+ cp 008h ;1dea
+ jr nc,error1 ;1dec
+ ret ;1dee
+sub_1defh:
+ call tst_EXPR ;1def
+ push bc ;1df2
+ push iy ;1df3
+ pop bc ;1df5
+ and a ;1df6
+ sbc hl,bc ;1df7
+ dec hl ;1df9
+ dec hl ;1dfa
+ pop bc ;1dfb
+ call sub_1e15h ;1dfc
+ ld a,h ;1dff
+ xor l ;1e00
+ bit 7,a ;1e01
+ jr nz,error1 ;1e03
+ ret ;1e05
+sub_1e06h:
+ ld a,(de) ;1e06
+ cp 028h ;1e07
+ jr nz,sub_1e12h ;1e09
+ inc de ;1e0b
+ call sub_1e12h ;1e0c
+ jp sub_1ed8h ;1e0f
+
+sub_1e12h:
+ call tst_EXPR ;1e12
+sub_1e15h:
+ ld a,h ;1e15
+ and a ;1e16
+ ret z ;1e17
+ inc a ;1e18
+ ret z ;1e19
+ jr error1 ;1e1a
+
+tst_EXPR:
+ push bc ;1e1c
+ call EXPR ;1e1d
+ pop bc ;1e20
+ ret nc ;1e21
+error1:
+ jp ERROR ;1e22
+sub_1e25h:
+ push hl ;1e25
+ ld hl,t_BC.DE.HL.AF ;1e26
+ jr l1e32h ;1e29
+sub_1e2bh:
+ push hl ;1e2b
+ jr l1e32h ;1e2c
+sub_1e2eh:
+ push hl ;1e2e
+ ld hl,t_BC.DE.HL.SP ;1e2f
+l1e32h:
+ push bc ;1e32
+ call sub_0a15h ;1e33
+ jr nc,l1e3eh ;1e36
+ ld a,b ;1e38
+ rlca ;1e39
+ rlca ;1e3a
+ rlca ;1e3b
+ rlca ;1e3c
+ scf ;1e3d
+l1e3eh:
+ pop bc ;1e3e
+ pop hl ;1e3f
+ ret ;1e40
+sub_1e41h:
+ call SKIPBL ;1e41
+ push bc ;1e44
+ push hl ;1e45
+ ld hl,t_BCDEHL_HL_A ;1e46
+ call sub_0a15h ;1e49
+ ld a,b ;1e4c
+ pop hl ;1e4d
+ pop bc ;1e4e
+ ret ;1e4f
+sub_1e50h:
+ push hl ;1e50
+ push bc ;1e51
+ ld hl,t_IX.IY ;1e52
+ call sub_0a15h ;1e55
+ jr nc,l1e65h ;1e58
+ ld a,0ddh ;1e5a
+ dec b ;1e5c
+ jr nz,l1e61h ;1e5d
+ ld a,0fdh ;1e5f
+l1e61h:
+ ld (pfx.IXY),a ;1e61
+ scf ;1e64
+l1e65h:
+ pop bc ;1e65
+ pop hl ;1e66
+ ret ;1e67
+sub_1e68h:
+ push hl ;1e68
+ push bc ;1e69
+ ld a,(de) ;1e6a
+ cp '(' ;1e6b
+ jr nz,l1eb4h ;1e6d
+ push de ;1e6f
+ inc de ;1e70
+ ld hl,t_IX.IY ;1e71
+ call sub_0a15h ;1e74
+ jr nc,l1eb3h ;1e77
+ pop af ;1e79
+ ld a,0ddh ;1e7a
+ dec b ;1e7c
+ jr nz,l1e81h ;1e7d
+ ld a,0fdh ;1e7f
+l1e81h:
+ ld (pfx.IXY),a ;1e81
+ ld a,(de) ;1e84
+ cp '+' ;1e85
+ jr z,l1e95h ;1e87
+ cp ')' ;1e89
+ ld hl,0 ;1e8b
+ jr z,l1eadh ;1e8e
+ cp '-' ;1e90
+ jp nz,ERROR ;1e92
+l1e95h:
+ push af ;1e95
+ inc de ;1e96
+ call sub_1e12h ;1e97 get displacement
+ pop af ;1e9a
+ cp '+' ;1e9b
+ jr z,l1ea7h ;1e9d
+ ld b,h ;1e9f
+ ld c,l ;1ea0
+ ld hl,0 ;1ea1
+ and a ;1ea4
+ sbc hl,bc ;1ea5
+l1ea7h:
+ ld a,(de) ;1ea7
+ cp ')' ;1ea8
+ jp nz,ERROR ;1eaa
+l1eadh:
+ inc de ;1ead
+ pop bc ;1eae
+ ld c,l ;1eaf
+ pop hl ;1eb0
+ scf ;1eb1
+ ret ;1eb2
+l1eb3h:
+ pop de ;1eb3
+l1eb4h:
+ pop bc ;1eb4
+ pop hl ;1eb5
+ and a ;1eb6
+ ret ;1eb7
+sub_1eb8h:
+ ld hl,t_tstfl_ZCPS ;1eb8
+ ld c,007h ;1ebb
+ jr l1ec4h ;1ebd
+sub_1ebfh:
+ ld hl,t_tstfl_ZC ;1ebf
+ ld c,003h ;1ec2
+l1ec4h:
+ push bc ;1ec4
+ call sub_0a15h ;1ec5
+ ld a,b ;1ec8
+ pop bc ;1ec9
+ ret nc ;1eca
+ and c ;1ecb
+ rlca ;1ecc
+ rlca ;1ecd
+ rlca ;1ece
+ scf ;1ecf
+ ret ;1ed0
+sub_1ed1h:
+ call skip_to_nextarg ;1ed1
+ ret z ;1ed4
+l1ed5h:
+ jp ERROR ;1ed5
+sub_1ed8h:
+ ld a,(de) ;1ed8
+ cp 029h ;1ed9
+ jr nz,l1ed5h ;1edb
+ inc de ;1edd
+ ret ;1ede
+CMD.L:
+ ld hl,CMD.L ;1edf
+ ld (CMD_RPT),hl ;1ee2
+ call EXPR ;1ee5
+ jr nc,l1eedh ;1ee8
+ ld hl,(lst.L) ;1eea
+l1eedh:
+ push hl ;1eed
+ pop iy ;1eee
+ call skip_to_nextarg ;1ef0
+ call sub_0aa5h ;1ef3
+ jr nc,l1f17h ;1ef6
+ call assert_eol ;1ef8
+ ld b,010h ;1efb
+l1efdh:
+ push bc ;1efd
+ push iy ;1efe
+ pop hl ;1f00
+ push hl ;1f01
+ call sub_1f3fh ;1f02
+ call CRLF ;1f05
+ pop iy ;1f08
+ ld c,b ;1f0a
+ ld b,000h ;1f0b
+ add iy,bc ;1f0d
+ ld (lst.L),iy ;1f0f
+ pop bc ;1f13
+ djnz l1efdh ;1f14
+ ret ;1f16
+l1f17h:
+ call assert_eol ;1f17
+ ld h,b ;1f1a
+ ld l,c ;1f1b
+ ld a,b ;1f1c
+ or c ;1f1d
+ jr nz,l1f21h ;1f1e
+ dec hl ;1f20
+l1f21h:
+ push hl ;1f21
+ push iy ;1f22
+ pop hl ;1f24
+ push hl ;1f25
+ call sub_1f3fh ;1f26
+ call CRLF ;1f29
+ pop iy ;1f2c
+ ld e,b ;1f2e
+ ld d,000h ;1f2f
+ add iy,de ;1f31
+ ld (lst.L),iy ;1f33
+ pop hl ;1f37
+ and a ;1f38
+ sbc hl,de ;1f39
+ ret z ;1f3b
+ ret c ;1f3c
+ jr l1f21h ;1f3d
+sub_1f3fh:
+ call out.hl.@ ;1f3f
+ call z,OUTBL ;1f42
+ call OUTBL ;1f45
+ sub a ;1f48
+ ld (CON.COL),a ;1f49
+ call sub_1f77h ;1f4c
+ and a ;1f4f
+ ret z ;1f50
+l1f51h:
+ call OUTBL ;1f51
+ ld a,(CON.COL) ;1f54
+ cp 010h ;1f57
+ jr c,l1f51h ;1f59
+sub_1f5bh:
+ ld de,(offs.@) ;1f5b
+ ld a,d ;1f5f
+ or e ;1f60
+ ret z ;1f61
+ ld a,'(' ;1f62
+ call OUTCHAR ;1f64
+ ld a,'@' ;1f67
+ call OUTCHAR ;1f69
+ and a ;1f6c
+ sbc hl,de ;1f6d
+ call out.hl ;1f6f
+ ld a,')' ;1f72
+ jp OUTCHAR ;1f74
+sub_1f77h:
+ sub a ;1f77
+ ld (XBE03),a ;1f78
+ call sub_1f9eh ;1f7b
+ jr nc,l1f91h ;1f7e
+ push bc ;1f80
+ call sub_2581h ;1f81
+ ex de,hl ;1f84
+ call sub_1fdbh ;1f85
+ pop bc ;1f88
+ ld a,(XBE03) ;1f89
+ ld hl,(XBE01) ;1f8c
+ scf ;1f8f
+ ret ;1f90
+l1f91h:
+ ld hl,b_0x1F9B_start ;1f91
+ call PSTR ;1f94
+ ld b,001h ;1f97
+ sub a ;1f99
+ ret ;1f9a
+
+b_0x1F9B_start:
+ DC '???'
+
+sub_1f9eh:
+ sub a ;1f9e
+ ld (is.pfx.IXY),a ;1f9f
+ comst ;1fa2
+ ld a,(iy+000h) ;1fa6
+ comend
+ cp 0edh ;1fa9
+ jp z,disas_pfx.ED ;1fab
+ cp 0ddh ;1fae
+ jr z,l1fc5h ;1fb0
+ cp 0fdh ;1fb2
+ jr z,l1fc9h ;1fb4
+sub_1fb6h:
+ comst ;1fb6
+ ld a,(iy+000h) ;1fba
+ comend
+ cp 0cbh ;1fbd
+ jp z,l2061h ;1fbf
+ jp l2078h ;1fc2
+l1fc5h:
+ ld a,001h ;1fc5
+ jr l1fcbh ;1fc7
+l1fc9h:
+ ld a,002h ;1fc9
+l1fcbh:
+ ld (is.pfx.IXY),a ;1fcb
+ call sub_1fdch ;1fce
+ ret nc ;1fd1
+ push bc ;1fd2
+ call sub_1fb6h ;1fd3
+ pop af ;1fd6
+ add a,b ;1fd7
+ ld b,a ;1fd8
+ scf ;1fd9
+ ret ;1fda
+
+sub_1fdbh:
+ jp (hl) ;1fdb
+
+sub_1fdch:
+ inc iy ;1fdc
+ ld hl,b_0x2011_start ;1fde
+ call sub_20bbh ;1fe1
+ ld b,002h ;1fe4
+ ret c ;1fe6
+ ld hl,l202ch ;1fe7
+ call sub_20bbh ;1fea
+ ld b,001h ;1fed
+ ret c ;1fef
+ comst ;1ff0
+ ld a,(iy+000h) ;1ff4
+ comend
+ cp 0cbh ;1ff7
+ jr nz,l200fh ;1ff9
+ comst ;1ffb
+ ld a,(iy+002h) ;1fff
+ comend
+ cp 036h ;2002
+ ret z ;2004
+ and 007h ;2005
+ cp 006h ;2007
+ jr nz,l200fh ;2009
+ ld b,002h ;200b
+ scf ;200d
+ ret ;200e
+l200fh:
+ and a ;200f
+ ret ;2010
+
+b_0x2011_start:
+ db 034h ;2011
+ db 035h ;2012
+ db 036h ;2013
+ db 046h ;2014
+ db 04eh ;2015
+ db 056h ;2016
+ db 05eh ;2017
+ db 066h ;2018
+ db 06eh ;2019
+ db 070h ;201a
+ db 071h ;201b
+ db 072h ;201c
+ db 073h ;201d
+ db 074h ;201e
+ db 075h ;201f
+ db 076h ;2020
+ db 077h ;2021
+ db 07eh ;2022
+ db 086h ;2023
+ db 08eh ;2024
+ db 096h ;2025
+ db 09eh ;2026
+ db 0a6h ;2027
+ db 0aeh ;2028
+ db 0b6h ;2029
+ db 0beh ;202a
+ db 000h ;202b
+l202ch:
+ db 009h ;202c
+ db 019h ;202d
+ db 021h ;202e
+ db 022h ;202f
+ db 023h ;2030
+ db 029h ;2031
+ db 02ah ;2032
+ db 02bh ;2033
+ db 039h ;2034
+ db 0e1h ;2035
+ db 0e3h ;2036
+ db 0e5h ;2037
+ db 0e9h ;2038
+ db 0f9h ;2039
+ db 000h ;203a
+
+disas_pfx.ED:
+ inc iy ;203b
+ ld hl,b_0x2200_start ;203d
+ call sub_209dh ;2040
+ ld b,002h ;2043
+ ret c ;2045
+ ld hl,l2235h ;2046
+ call lookup_opc ;2049
+ ld b,002h ;204c
+ ret c ;204e
+ ld hl,l228bh ;204f
+ call lookup_opc ;2052
+ ld b,003h ;2055
+ ret c ;2057
+ ld hl,l22b4h ;2058
+ call lookup_opc ;205b
+ ld b,004h ;205e
+ ret ;2060
+l2061h:
+ push iy ;2061
+ inc iy ;2063
+ ld a,(is.pfx.IXY) ;2065
+ and a ;2068
+ jr z,l206dh ;2069
+ inc iy ;206b
+l206dh:
+ ld hl,l22c9h ;206d
+ call lookup_opc ;2070
+ pop iy ;2073
+ ld b,002h ;2075
+ ret ;2077
+l2078h:
+ ld hl,b_0x218B_start ;2078
+ call lookup_opc ;207b
+ ld b,002h ;207e
+ ret c ;2080
+ ld hl,b_0x20ED_start ;2081
+ call sub_209dh ;2084
+ ld b,001h ;2087
+ ret c ;2089
+ ld hl,b_0x2108_start ;208a
+ call lookup_opc ;208d
+ ld b,001h ;2090
+ ret c ;2092
+ ld hl,b_0x21D2_start ;2093
+ call lookup_opc ;2096
+ ret nc ;2099
+ ld b,003h ;209a
+ ret ;209c
+
+sub_209dh:
+ ld a,(hl) ;209d
+ cp 0ffh ;209e
+ ret z ;20a0
+ comst ;20a1
+ cp (iy+000h) ;20a5
+ comend
+ jr z,l20aeh ;20a8
+ inc hl ;20aa
+ inc hl ;20ab
+ jr sub_209dh ;20ac
+l20aeh:
+ inc hl ;20ae
+ ld c,(hl) ;20af
+ ld hl,t_MNEMONICS ;20b0
+ ld b,000h ;20b3
+ add hl,bc ;20b5
+ ld de,l230bh ;20b6
+ scf ;20b9
+ ret ;20ba
+sub_20bbh:
+ ld a,(hl) ;20bb
+ and a ;20bc
+ ret z ;20bd
+ inc hl ;20be
+ comst ;20bf
+ cp (iy+000h) ;20c3
+ comend
+ jr nz,sub_20bbh ;20c6
+ scf ;20c8
+ ret ;20c9
+
+lookup_opc:
+ comst ;20ca
+ ld a,(iy+000h) ;20ce
+ comend
+ and (hl) ;20d1
+ inc hl ;20d2
+ cp (hl) ;20d3
+ jr z,l20dfh ;20d4
+ inc hl ;20d6
+ inc hl ;20d7
+ inc hl ;20d8
+ inc hl ;20d9
+ ld a,(hl) ;20da
+ and a ;20db
+ jr nz,lookup_opc ;20dc
+ ret ;20de
+l20dfh:
+ inc hl ;20df
+ ld c,(hl) ;20e0
+ inc hl ;20e1
+ ld e,(hl) ;20e2
+ inc hl ;20e3
+ ld d,(hl) ;20e4
+ ld hl,t_MNEMONICS ;20e5
+ ld b,000h ;20e8
+ add hl,bc ;20ea
+ scf ;20eb
+ ret ;20ec
+
+b_0x20ED_start: ; 1 byte opcodes (no parameters)
+ db 076h ;20ed halt
+ db 039h ;20ee
+ db 0d9h ;20ef exx
+ db 036h ;20f0
+ db 0f3h ;20f1 di
+ db 02ch ;20f2
+ db 0fbh ;20f3 ei
+ db 032h ;20f4
+ db 000h ;20f5 nop
+ db 069h ;20f6
+ db 007h ;20f7 rlca
+ db 09eh ;20f8
+ db 00fh ;20f9 rrca
+ db 0adh ;20fa
+ db 017h ;20fb rla
+ db 098h ;20fc
+ db 01fh ;20fd rra
+ db 0a7h ;20fe
+ db 027h ;20ff daa
+ db 026h ;2100
+ db 02fh ;2101 cpl
+ db 023h ;2102
+ db 037h ;2103 scf
+ db 0bah ;2104
+ db 03fh ;2105 ccf
+ db 010h ;2106
+ db 0ffh ;2107 EOT
+
+b_0x2108_start: ; 1 byte opcodes
+ defb 0c0h ;2108 ld r,r
+ defb 040h ;2109
+ defb 056h ;210a
+ defw l22fch ;210b
+
+ defb 0f8h ;210d add a,r
+ defb 080h ;210e
+ defb 003h ;210f
+ defw l2305h ;2110
+
+ defb 0f8h ;2112 adc a,r
+ defb 088h ;2113
+ defb 000h ;2114
+ defw l2305h ;2115
+
+ defb 0f8h ;2117
+ defb 090h ;2118
+ defb 0c9h ;2119
+ defw l24ebh ;211a
+
+ defb 0f8h ;211c
+ defb 098h ;211d
+ defb 0b7h ;211e
+ defw l2305h ;211f
+
+ defb 0f8h ;2121
+ defb 0a0h ;2122
+ defb 006h ;2123
+ defw l24ebh ;2124
+
+ defb 0f8h ;2126
+ defb 0a8h ;2127
+ defb 0cch ;2128
+ defw l24ebh ;2129
+
+ defb 0f8h ;212b
+ defb 0b0h ;212c
+ defb 06ch ;212d
+ defw l24ebh ;212e
+
+ defb 0f8h ;2130
+ defb 0b8h ;2131
+ defb 013h ;2132
+ defw l24ebh ;2133
+
+ defb 0c7h ;2135
+ defb 0c0h ;2136 ret cc
+ defb 08bh ;2137
+ defw l2561h ;2138
+
+ defb 0c7h ;213a rst
+ defb 0c7h ;213b
+ defb 0b4h ;213c
+ defw l231eh ;213d
+
+ defb 0ffh ;213f ret
+ defb 0c9h ;2140
+ defb 08bh ;2141
+ defw l230bh ;2142
+
+ defb 0cfh ;2144 pop rr
+ defb 0c1h ;2145
+ defb 081h ;2146
+ defw l2546h ;2147
+
+ defb 0cfh ;2149 push rr
+ defb 0c5h ;214a
+ defb 084h ;214b
+ defw l2546h ;214c
+
+ defb 0ffh ;214e ex (sp),hl
+ defb 0e3h ;214f
+ defb 034h ;2150
+ defw l232ah ;2151
+
+ defb 0ffh ;2153 jp (hl)
+ defb 0e9h ;2154
+ defb 052h ;2155
+ defw l2338h ;2156
+
+ defb 0ffh ;2158 ex de,hl
+ defb 0ebh ;2159
+ defb 034h ;215a
+ defw l2345h ;215b
+
+ defb 0ffh ;215d ld sp,hl
+ defb 0f9h ;215e
+ defb 056h ;215f
+ defw l234bh ;2160
+
+ defb 0cfh ;2162 inc rr
+ defb 003h ;2163
+ defb 041h ;2164
+ defw l254bh ;2165
+
+ defb 0cfh ;2167 dec rr
+ defb 00bh ;2168
+ defb 029h ;2169
+ defw l254bh ;216a
+
+ defb 0c7h ;216c inc r
+ defb 004h ;216d
+ defb 041h ;216e
+ defw l24dfh ;216f
+
+ defb 0c7h ;2171 dec r
+ defb 005h ;2172
+ defb 029h ;2173
+ defw l24dfh ;2174
+
+ defb 0ffh ;2176 ex af,af'
+ defb 008h ;2177
+ defb 034h ;2178
+ defw l2357h ;2179
+
+ defb 0cfh ;217b add hl,rr
+ defb 009h ;217c
+ defb 003h ;217d
+ defw l235dh ;217e
+
+ defb 0efh ;2180 ld (rr),a ;rr=bc,de
+ defb 002h ;2181
+ defb 056h ;2182
+ defw l2366h ;2183
+
+ defb 0efh ;2185 ld a,(rr) ;rr=bc,de
+ defb 00ah ;2186
+ defb 056h ;2187
+ defw l236fh ;2188
+
+ defb 000h ;218a EOT
+
+b_0x218B_start: ; 2 byte opdodes
+ defb 0c7h ;218b ld r,nn
+ defb 006h ;218c
+ defb 056h ;218d
+ defw l2384h ;218e
+
+ defb 0ffh ;2190 add a,nn
+ defb 0c6h ;2191
+ defb 003h ;2192
+ defw l237fh ;2193
+
+ defb 0ffh ;2195 adc a,nn
+ defb 0ceh ;2196
+ defb 000h ;2197
+ defw l237fh ;2198
+
+ defb 0ffh ;219a sub a,nn
+ defb 0d6h ;219b
+ defb 0c9h ;219c
+ defw l2397h ;219d
+
+ defb 0ffh ;219f
+ defb 0deh ;21a0
+ defb 0b7h ;21a1
+ defw l237fh ;21a2
+
+ defb 0ffh ;21a4 and a,nn
+ defb 0e6h ;21a5
+ defb 006h ;21a6
+ defw l2397h ;21a7
+
+ defb 0ffh ;21a9
+ defb 0eeh ;21aa
+ defb 0cch ;21ab
+ defw l2397h ;21ac
+
+ defb 0ffh ;21ae
+ defb 0f6h ;21af
+ defb 06ch ;21b0
+ defw l2397h ;21b1
+
+ defb 0ffh ;21b3 cp a,nn
+ defb 0feh ;21b4
+ defb 013h ;21b5
+ defw l2397h ;21b6
+
+ defb 0ffh ;21b8 djnz
+ defb 010h ;21b9
+ defb 02eh ;21ba
+ defw l23b0h ;21bb
+
+ defb 0ffh ;21bd jr
+ defb 018h ;21be
+ defb 054h ;21bf
+ defw l23b0h ;21c0
+
+ defb 0e7h ;21c2 jr,cc
+ defb 020h ;21c3
+ defb 054h ;21c4
+ defw l23a1h ;21c5
+
+ defb 0ffh ;21c7
+ defb 0d3h ;21c8 out (nn),a
+ defb 076h ;21c9
+ defw l23d5h ;21ca
+
+ defb 0ffh ;21cc in a,(nn)
+ defb 0dbh ;21cd
+ defb 03fh ;21ce
+ defw l23c3h ;21cf
+
+ defb 000h ;21d1 EOT
+
+b_0x21D2_start: ; 3 byte opcodes
+ defb 0c7h ;21d2
+ defb 0c2h ;21d3
+ defb 052h ;21d4
+ defw l23e0h ;21d5
+
+ defb 0c7h ;21d7
+ defb 0c4h ;21d8
+ defb 00ch ;21d9
+ defw l23e0h ;21da
+
+ defb 0cfh ;21dc
+ defb 001h ;21dd
+ defb 056h ;21de
+ defw l23fch ;21df
+
+ defb 0ffh ;21e1
+ defb 0c3h ;21e2
+ defb 052h ;21e3
+ defw l23e6h ;21e4
+
+ defb 0ffh ;21e6
+ defb 0cdh ;21e7
+ defb 00ch ;21e8
+ defw l23e6h ;21e9
+
+ defb 0ffh ;21eb
+ defb 022h ;21ec
+ defb 056h ;21ed
+ defw l2404h ;21ee
+
+ defb 0ffh ;21f0
+ defb 02ah ;21f1
+ defb 056h ;21f2
+ defw l240dh ;21f3
+
+ defb 0ffh ;21f5
+ defb 032h ;21f6
+ defb 056h ;21f7
+ defw l2416h ;21f8
+
+ defb 0ffh ;21fa
+ defb 03ah ;21fb
+ defb 056h ;21fc
+ defw l2421h ;21fd
+
+ defb 000h ;21ff
+
+b_0x2200_start: ; prefix ED + 1 byte opcode
+ defb 044h ;2200 neg
+ defb 066h ;2201
+ defb 045h ;2202 retn
+ defb 092h ;2203
+ defb 04dh ;2204 reti
+ defb 08eh ;2205
+ defb 067h ;2206 rrd
+ defb 0b1h ;2207
+ defb 06fh ;2208 rld
+ defb 0a2h ;2209
+ defb 0a0h ;220a ldi
+ defb 05fh ;220b
+ defb 0a1h ;220c
+ defb 01ch ;220d
+ defb 0a2h ;220e
+ defb 04bh ;220f
+ defb 0a3h ;2210
+ defb 07dh ;2211
+ defb 0a8h ;2212 ldd
+ defb 058h ;2213
+ defb 0a9h ;2214
+ defb 015h ;2215
+ defb 0aah ;2216
+ defb 044h ;2217
+ defb 0abh ;2218
+ defb 079h ;2219
+ defb 0b0h ;221a ldir
+ defb 062h ;221b
+ defb 0b1h ;221c
+ defb 01fh ;221d
+ defb 0b2h ;221e
+ defb 04eh ;221f
+ defb 0b3h ;2220
+ defb 072h ;2221
+ defb 0b8h ;2222 lddr
+ defb 05bh ;2223
+ defb 0b9h ;2224
+ defb 018h ;2225
+ defb 0bah ;2226
+ defb 047h ;2227
+ defb 0bbh ;2228
+ defb 06eh ;2229
+ defb 08bh ;222a otdm
+ defb 0d5h ;222b
+ defb 09bh ;222c otdmr
+ defb 0d9h ;222d
+ defb 083h ;222e otim
+ defb 0deh ;222f
+ defb 093h ;2230 otimr
+ defb 0e2h ;2231
+ defb 076h ;2232 slp
+ defb 0ebh ;2233
+ defb 0ffh ;2234 EOT
+
+l2235h:
+ defb 0e7h ;2235 in r,(c) ;r=bcde
+ defb 040h ;2236
+ defb 03fh ;2237
+ defw l2455h ;2238
+
+ defb 0f7h ;223a in r,(c) ;r=hl
+ defb 060h ;223b
+ defb 03fh ;223c
+ defw l2455h ;223d
+
+ defb 0ffh ;223f in r,(c) ;r=a
+ defb 078h ;2240
+ defb 03fh ;2241
+ defw l2455h ;2242
+
+ defb 0e7h ;2244
+ defb 041h ;2245
+ defb 076h ;2246
+ defw l2461h ;2247
+
+ defb 0f7h ;2249
+ defb 061h ;224a
+ defb 076h ;224b
+ defw l2461h ;224c
+
+ defb 0ffh ;224e out (c),r ;r=a
+ defb 079h ;224f
+ defb 076h ;2250
+ defw l2461h ;2251
+
+ defb 0cfh ;2253 sbc hl,rr
+ defb 042h ;2254
+ defb 0b7h ;2255
+ defw l246dh ;2256
+
+ defb 0cfh ;2258 adc hl,rr
+ defb 04ah ;2259
+ defb 000h ;225a
+ defw l246dh ;225b
+
+ defb 0ffh ;225d im 0
+ defb 046h ;225e
+ defb 03dh ;225f
+ defw l2427h ;2260
+
+ defb 0ffh ;2262 im 1
+ defb 056h ;2263
+ defb 03dh ;2264
+ defw l242bh ;2265
+
+ defb 0ffh ;2267 im 2
+ defb 05eh ;2268
+ defb 03dh ;2269
+ defw l242fh ;226a
+
+ defb 0ffh ;226c ld i,a
+ defb 047h ;226d
+ defb 056h ;226e
+ defw l2434h ;226f
+
+ defb 0ffh ;2271
+ defb 057h ;2272
+ defb 056h ;2273
+ defw l2439h ;2274
+
+ defb 0ffh ;2276
+ defb 04fh ;2277
+ defb 056h ;2278
+ defw l243eh ;2279
+
+ defb 0ffh ;227b
+ defb 05fh ;227c
+ defb 056h ;227d
+ defw l2443h ;227e
+
+ defb 0cfh ;2280 mlt rr
+ defb 04ch ;2281
+ defb 0d2h ;2282
+ defw l254bh ;2283
+
+ defb 0c7h ;2285 tst r
+ defb 004h ;2286
+ defb 0eeh ;2287
+ defw l24dfh ;2288
+
+ defb 000h ;228a
+
+l228bh:
+ defb 0e7h ;228b
+ defb 000h ;228c
+ defb 0cfh ;228d
+
+b_0x228E_start:
+ defw l230ch ;228e
+
+b_0x2290_start:
+ defb 0f7h ;2290
+ defb 020h ;2291
+ defb 0cfh ;2292
+
+b_0x2293_start:
+ defw l230ch ;2293
+
+b_0x2295_start:
+ defb 0ffh ;2295
+ defb 038h ;2296
+ defb 0cfh ;2297
+
+b_0x2298_start:
+ defw l230ch ;2298
+
+b_0x229A_start:
+ defb 0e7h ;229a
+ defb 001h ;229b
+ defb 0e7h ;229c
+
+b_0x229D_start:
+ defw l2315h ;229d
+
+b_0x229F_start:
+ defb 0f7h ;229f
+ defb 021h ;22a0
+ defb 0e7h ;22a1
+
+b_0x22A2_start:
+ defw l2315h ;22a2
+
+b_0x22A4_start:
+ defb 0ffh ;22a4
+ defb 039h ;22a5
+ defb 0e7h ;22a6
+
+b_0x22A7_start:
+ defw l2315h ;22a7
+
+b_0x22A9_start:
+ defb 0ffh ;22a9
+ defb 064h ;22aa
+ defb 0eeh ;22ab
+
+b_0x22AC_start:
+ defw l2397h ;22ac
+
+b_0x22AE_start:
+ defb 0ffh ;22ae
+ defb 074h ;22af
+ defb 0f1h ;22b0
+
+b_0x22B1_start:
+ defw l2397h ;22b1
+
+b_0x22B3_start:
+ defb 000h ;22b3
+l22b4h:
+ defb 0efh ;22b4
+ defb 043h ;22b5
+ defb 056h ;22b6
+
+b_0x22B7_start:
+ defw l2476h ;22b7
+
+b_0x22B9_start:
+ defb 0ffh ;22b9
+ defb 073h ;22ba
+ defb 056h ;22bb
+
+b_0x22BC_start:
+ defw l2476h ;22bc
+
+b_0x22BE_start:
+ defb 0efh ;22be
+ defb 04bh ;22bf
+ defb 056h ;22c0
+
+b_0x22C1_start:
+ defw l247fh ;22c1
+
+b_0x22C3_start:
+ defb 0ffh ;22c3
+ defb 07bh ;22c4
+ defb 056h ;22c5
+
+b_0x22C6_start:
+ defw l247fh ;22c6
+
+b_0x22C8_start:
+ defb 000h ;22c8
+l22c9h:
+ defb 0f8h ;22c9
+ defb 000h ;22ca
+ defb 09bh ;22cb
+
+b_0x22CC_start:
+ defw l24aeh ;22cc
+
+b_0x22CE_start:
+ defb 0f8h ;22ce
+ defb 008h ;22cf
+ defb 0aah ;22d0
+
+b_0x22D1_start:
+ defw l24aeh ;22d1
+
+b_0x22D3_start:
+ defb 0f8h ;22d3
+ defb 010h ;22d4
+ defb 096h ;22d5
+
+b_0x22D6_start:
+ defw l24aeh ;22d6
+
+b_0x22D8_start:
+ defb 0f8h ;22d8
+ defb 018h ;22d9
+ defb 0a5h ;22da
+
+b_0x22DB_start:
+ defw l24aeh ;22db
+
+b_0x22DD_start:
+ defb 0f8h ;22dd
+ defb 020h ;22de
+ defb 0c0h ;22df
+
+b_0x22E0_start:
+ defw l24aeh ;22e0
+
+b_0x22E2_start:
+ defb 0f8h ;22e2
+ defb 028h ;22e3
+ defb 0c3h ;22e4
+
+b_0x22E5_start:
+ defw l24aeh ;22e5
+
+b_0x22E7_start:
+ defb 0f8h ;22e7
+ defb 038h ;22e8
+ defb 0c6h ;22e9
+
+b_0x22EA_start:
+ defw l24aeh ;22ea
+
+b_0x22EC_start:
+ defb 0c0h ;22ec
+ defb 040h ;22ed
+ defb 009h ;22ee
+
+b_0x22EF_start:
+ defw l2487h ;22ef
+
+b_0x22F1_start:
+ defb 0c0h ;22f1
+ defb 080h ;22f2
+ defb 088h ;22f3
+
+b_0x22F4_start:
+ defw l2487h ;22f4
+
+b_0x22F6_start:
+ defb 0c0h ;22f6
+ defb 0c0h ;22f7
+ defb 0bdh ;22f8
+
+b_0x22F9_start:
+ defw l2487h ;22f9
+
+b_0x22FB_start:
+ defb 000h ;22fb
+l22fch:
+ call l24dfh ;22fc
+ call sub_257ch ;22ff
+ jp l24ebh ;2302
+l2305h:
+ call sub_2579h ;2305
+ jp l24ebh ;2308
+l230bh:
+ ret ;230b
+l230ch:
+ call l24dfh ;230c
+ call sub_257ch ;230f
+ jp l23c6h ;2312
+l2315h:
+ call l23c6h ;2315
+ call sub_257ch ;2318
+ jp l24dfh ;231b
+l231eh:
+ comst ;231e
+ ld a,(iy+000h) ;2322
+ comend
+ and 038h ;2325
+ jp out.hex ;2327
+l232ah:
+ ld hl,b_0x2333_start ;232a
+ call PSTR ;232d
+ jp l253eh ;2330
+
+b_0x2333_start:
+ DC '(SP),'
+
+l2338h:
+ ld a,'(' ;2338
+ call OUTCHAR ;233a
+ call l253eh ;233d
+ ld a,')' ;2340
+ jp OUTCHAR ;2342
+l2345h:
+ ld hl,l1d86h ;2345
+ jp PSTR ;2348
+l234bh:
+ ld hl,b_0x2354_start ;234b
+ call PSTR ;234e
+ jp l253eh ;2351
+
+b_0x2354_start:
+ DC 'SP,'
+
+l2357h:
+ ld hl,b_0x1D80_start ;2357
+ jp PSTR ;235a
+l235dh:
+ call l253eh ;235d
+ call sub_257ch ;2360
+ jp l254bh ;2363
+l2366h:
+ call sub_2372h ;2366
+ call sub_257ch ;2369
+ jp l23dbh ;236c
+l236fh:
+ call sub_2579h ;236f
+sub_2372h:
+ ld a,'(' ;2372
+ call OUTCHAR ;2374
+ call l254bh ;2377
+ ld a,')' ;237a
+ jp OUTCHAR ;237c
+l237fh:
+ call sub_2579h ;237f
+ jr l2397h ;2382
+l2384h:
+ call l24dfh ;2384
+ call sub_257ch ;2387
+ ld a,(is.pfx.IXY) ;238a
+ and a ;238d
+ comst ;238e
+ ld a,(iy+002h) ;2392
+ comend
+ jr nz,l239eh ;2395
+l2397h:
+ comst ;2397
+ ld a,(iy+001h) ;239b
+ comend
+l239eh:
+ jp out.hex ;239e
+l23a1h:
+ comst ;23a1
+ ld a,(iy+000h) ;23a5
+ comend
+ and 018h ;23a8
+ call sub_2568h ;23aa
+ call sub_257ch ;23ad
+l23b0h:
+ comst ;23b0
+ ld c,(iy+001h) ;23b4
+ comend
+ ld a,c ;23b7
+ rla ;23b8
+ sbc a,a ;23b9
+ ld b,a ;23ba
+ push iy ;23bb
+ pop hl ;23bd
+ add hl,bc ;23be
+ inc hl ;23bf
+ inc hl ;23c0
+ jr l23f0h ;23c1
+l23c3h:
+ call sub_2579h ;23c3
+l23c6h:
+ ld a,028h ;23c6
+ call OUTCHAR ;23c8
+ comst ;23cb
+ ld a,(iy+001h) ;23cf
+ comend
+ jp l252bh ;23d2
+l23d5h:
+ call l23c6h ;23d5
+ call sub_257ch ;23d8
+l23dbh:
+ ld a,041h ;23db
+ jp OUTCHAR ;23dd
+l23e0h:
+ call l2561h ;23e0
+ call sub_257ch ;23e3
+l23e6h:
+ comst ;23e6
+ ld l,(iy+001h) ;23ea
+ ld h,(iy+002h) ;23ed
+ comend
+l23f0h:
+ ld a,002h ;23f0
+sub_23f2h:
+ ld (XBE03),a ;23f2
+ ld (XBE01),hl ;23f5
+ call out.hl ;23f8
+ ret ;23fb
+l23fch:
+ call l254bh ;23fc
+ call sub_257ch ;23ff
+ jr l23e6h ;2402
+l2404h:
+ call sub_24c6h ;2404
+ call sub_257ch ;2407
+ jp l253eh ;240a
+l240dh:
+ call l253eh ;240d
+ call sub_257ch ;2410
+ jp sub_24c6h ;2413
+l2416h:
+ call sub_24c6h ;2416
+ call sub_257ch ;2419
+ ld a,041h ;241c
+ jp OUTCHAR ;241e
+l2421h:
+ call sub_2579h ;2421
+ jp sub_24c6h ;2424
+l2427h:
+ ld a,030h ;2427
+ jr l2431h ;2429
+l242bh:
+ ld a,031h ;242b
+ jr l2431h ;242d
+l242fh:
+ ld a,032h ;242f
+l2431h:
+ jp OUTCHAR ;2431
+l2434h:
+ ld hl,b_0x2449_start ;2434
+ jr l2446h ;2437
+l2439h:
+ ld hl,l244ch ;2439
+ jr l2446h ;243c
+l243eh:
+ ld hl,l244fh ;243e
+ jr l2446h ;2441
+l2443h:
+ ld hl,l2452h ;2443
+l2446h:
+ jp PSTR ;2446
+
+b_0x2449_start:
+ DC 'I,A'
+l244ch:
+ DC 'A,I'
+l244fh:
+ DC 'R,A'
+l2452h:
+ DC 'A,R'
+
+l2455h:
+ call l24dfh ;2455
+ call sub_257ch ;2458
+ ld hl,t__C_ ;245b
+ jp PSTR ;245e
+l2461h:
+ ld hl,t__C_ ;2461
+ call PSTR ;2464
+ call sub_257ch ;2467
+ jp l24dfh ;246a
+l246dh:
+ call l253eh ;246d
+ call sub_257ch ;2470
+ jp l254bh ;2473
+l2476h:
+ call sub_24c6h ;2476
+ call sub_257ch ;2479
+ jp l254bh ;247c
+l247fh:
+ call l254bh ;247f
+ call sub_257ch ;2482
+ jr sub_24c6h ;2485
+l2487h:
+ ld a,(is.pfx.IXY) ;2487
+ and a ;248a
+ jr nz,l2496h ;248b
+ comst ;248d
+ ld a,(iy+001h) ;2491
+ comend
+ jr l249dh ;2494
+l2496h:
+ comst ;2496
+ ld a,(iy+002h) ;249a
+ comend
+l249dh:
+ push af ;249d
+ rra ;249e
+ rra ;249f
+ rra ;24a0
+ and 007h ;24a1
+ add a,'0' ;24a3
+ call OUTCHAR ;24a5
+ call sub_257ch ;24a8
+ pop af ;24ab
+ jr l24f2h ;24ac
+l24aeh:
+ ld a,(is.pfx.IXY) ;24ae
+ and a ;24b1
+ jr nz,l24bdh ;24b2
+ comst ;24b4
+ ld a,(iy+001h) ;24b8
+ comend
+ jr l24c4h ;24bb
+l24bdh:
+ comst ;24bd
+ ld a,(iy+002h) ;24c1
+ comend
+l24c4h:
+ jr l24f2h ;24c4
+sub_24c6h:
+ ld a,'(' ;24c6
+ call OUTCHAR ;24c8
+ comst ;24cb
+ ld l,(iy+001h) ;24cf
+ ld h,(iy+002h) ;24d2
+ comend
+ ld a,001h ;24d5
+ call sub_23f2h ;24d7
+ ld a,')' ;24da
+ jp OUTCHAR ;24dc
+l24dfh:
+ comst ;24df
+ ld a,(iy+000h) ;24e3
+ comend
+ rra ;24e6
+ rra ;24e7
+ rra ;24e8
+ jr l24f2h ;24e9
+l24ebh:
+ comst ;24eb
+ ld a,(iy+000h) ;24ef
+ comend
+l24f2h:
+ and 007h ;24f2
+ cp 006h ;24f4
+ jr nz,l2533h ;24f6
+ ld a,(is.pfx.IXY) ;24f8
+ and a ;24fb
+ ld a,006h ;24fc
+ jr z,l2533h ;24fe
+ ld hl,b_0x2538_start ;2500
+ ld a,(is.pfx.IXY) ;2503
+ dec a ;2506
+ jr z,l250ch ;2507
+ ld hl,b_0x253B_start ;2509
+l250ch:
+ call PSTR ;250c
+ comst ;250f
+ ld a,(iy+001h) ;2513
+ comend
+ and a ;2516
+ push af ;2517
+ jp m,l2523h ;2518
+ ld a,'+' ;251b
+ call OUTCHAR ;251d
+ pop af ;2520
+ jr l252bh ;2521
+l2523h:
+ ld a,'-' ;2523
+ call OUTCHAR ;2525
+ pop af ;2528
+ neg ;2529
+l252bh:
+ call out.hex ;252b
+ ld a,')' ;252e
+ jp OUTCHAR ;2530
+l2533h:
+ ld hl,t_BCDEHL_HL_A ;2533
+ jr l2572h ;2536
+
+b_0x2538_start:
+ DC '(IX'
+b_0x253B_start:
+ DC '(IY'
+
+l253eh:
+ ld a,(is.pfx.IXY) ;253e
+ ld hl,t_HL.IX.IY ;2541
+ jr l2572h ;2544
+l2546h:
+ ld hl,t_BC.DE.HL.AF ;2546
+ jr l254eh ;2549
+l254bh:
+ ld hl,t_BC.DE.HL.SP ;254b
+l254eh:
+ comst ;254e
+ ld a,(iy+000h) ;2552
+ comend
+ rra ;2555
+ rra ;2556
+ rra ;2557
+ rra ;2558
+ and 003h ;2559
+ cp 002h ;255b
+ jr z,l253eh ;255d
+ jr l2572h ;255f
+l2561h:
+ comst ;2561
+ ld a,(iy+000h) ;2565
+ comend
+sub_2568h:
+ rra ;2568
+ rra ;2569
+ rra ;256a
+ and 007h ;256b
+ ld hl,t_tstfl_ZCPS ;256d
+ jr l2572h ;2570
+l2572h:
+ ld b,a ;2572
+ call sub_0a48h ;2573
+ jp PSTR ;2576
+sub_2579h:
+ call l23dbh ;2579
+sub_257ch:
+ ld a,',' ;257c
+ jp OUTCHAR ;257e
+sub_2581h:
+ call PSTR ;2581
+l2584h:
+ call OUTBL ;2584
+ inc c ;2587
+ ld a,c ;2588
+ cp 006h ;2589
+ jr nz,l2584h ;258b
+ ret ;258d
+
+t_MNEMONICS:
+ DC 'ADC'
+ DC 'ADD'
+ DC 'AND'
+ DC 'BIT'
+ DC 'CALL'
+ DC 'CCF'
+ DC 'CP'
+ DC 'CPD'
+ DC 'CPDR'
+ DC 'CPI'
+ DC 'CPIR'
+ DC 'CPL'
+ DC 'DAA'
+ DC 'DEC'
+ DC 'DI'
+ DC 'DJNZ'
+ DC 'EI'
+ DC 'EX'
+ DC 'EXX'
+ DC 'HALT'
+ DC 'IM'
+ DC 'IN'
+ DC 'INC'
+ DC 'IND'
+ DC 'INDR'
+ DC 'INI'
+ DC 'INIR'
+ DC 'JP'
+ DC 'JR'
+ DC 'LD'
+ DC 'LDD'
+ DC 'LDDR'
+ DC 'LDI'
+ DC 'LDIR'
+ DC 'NEG'
+ DC 'NOP'
+ DC 'OR'
+ DC 'OTDR'
+ DC 'OTIR'
+ DC 'OUT'
+ DC 'OUTD'
+ DC 'OUTI'
+ DC 'POP'
+ DC 'PUSH'
+ DC 'RES'
+ DC 'RET'
+ DC 'RETI'
+ DC 'RETN'
+ DC 'RL'
+ DC 'RLA'
+ DC 'RLC'
+ DC 'RLCA'
+ DC 'RLD'
+ DC 'RR'
+ DC 'RRA'
+ DC 'RRC'
+ DC 'RRCA'
+ DC 'RRD'
+ DC 'RST'
+ DC 'SBC'
+ DC 'SCF'
+ DC 'SET'
+ DC 'SLA'
+ DC 'SRA'
+ DC 'SRL'
+ DC 'SUB'
+ DC 'XOR'
+ DC 'IN0'
+ DC 'MLT'
+ DC 'OTDM'
+ DC 'OTDMR'
+ DC 'OTIM'
+ DC 'OTIMR'
+ DC 'OUT0'
+ DC 'SLP'
+ DC 'TST'
+ DC 'TSTIO'
+ DB 0
+
+t_BCDEHL_HL_A:
+ DC 'B'
+ DC 'C'
+ DC 'D'
+ DC 'E'
+ DC 'H'
+ DC 'L'
+ DC '(HL)'
+ DC 'A'
+ DB 0
+t_BC.DE.HL.SP:
+ DC 'BC'
+ DC 'DE'
+ DC 'HL'
+ DC 'SP'
+ DB 0
+t_BC.DE.HL.AF:
+ DC 'BC'
+ DC 'DE'
+t_HL.AF:
+ DC 'HL'
+ DC 'AF'
+ DB 0
+t_BC.DE.IY.SP:
+ DC 'BC'
+ DC 'DE'
+ DC 'IY'
+ DC 'SP'
+ DB 0
+t_BC.DE.IX.SP:
+ DC 'BC'
+ DC 'DE'
+ DC 'IX'
+ DC 'SP'
+ DB 0
+t_HL.IX.IY:
+ DC 'HL'
+t_IX.IY:
+ DC 'IX'
+ DC 'IY'
+ DB 0
+t_tstfl_ZC:
+ DC 'NZ'
+ DC 'Z'
+ DC 'NC'
+ DC 'C'
+ DC 'NE'
+ DC 'EQ'
+ DC 'GE'
+ DC 'LT'
+ DB 0
+t_tstfl_ZCPS:
+ DC 'NZ'
+ DC 'Z'
+ DC 'NC'
+ DC 'C'
+ DC 'PO'
+ DC 'PE'
+ DC 'P'
+ DC 'M'
+ DC 'NE'
+ DC 'EQ'
+ DC 'GE'
+ DC 'LT'
+ DC 'NV'
+ DC 'V'
+ DB 0
+t__C_:
+ DC '(C)'
+ DB 0
+
+sub_26e7h:
+ ld hl,(REG.PC) ;26e7
+ ld a,h ;26ea
+ or l ;26eb
+ jr z,l2715h ;26ec
+ ld iy,(REG.PC) ;26ee
+ call sub_1f9eh ;26f2
+ jp nc,ERROR ;26f5
+ ld c,b ;26f8
+ ld b,000h ;26f9
+ ld hl,(REG.PC) ;26fb
+ add hl,bc ;26fe
+ call sub_1117h ;26ff
+ ld iy,(REG.PC) ;2702
+ ld hl,b_0x2717_start ;2706
+ call lookup_opc ;2709
+ ccf ;270c
+ ret c ;270d
+ ex de,hl ;270e
+ call CALL.HL ;270f
+ call c,sub_1117h ;2712
+l2715h:
+ scf ;2715
+ ret ;2716
+
+b_0x2717_start:
+ db 0ffh ;2717
+ db 0ddh ;2718
+ db 000h ;2719
+ dw x278d
+
+ db 0ffh ;271c
+ db 0fdh ;271d
+ db 000h ;271e
+ dw x2792
+
+ db 0ffh ;2721
+ db 0edh ;2722
+ db 000h ;2723
+ dw x27a2
+
+l2726h:
+ db 0ffh ;2726
+ db 0cdh ;2727
+ db 000h ;2728
+ dw x275e
+
+ db 0ffh ;272b
+ db 0c3h ;272c
+ db 000h ;272d
+ dw x2769
+
+ db 0ffh ;2730
+ db 0e9h ;2731
+ db 000h ;2732
+ dw x2788
+
+ db 0ffh ;2735
+ db 0c9h ;2736
+ db 000h ;2737
+ dw x27c9
+
+ db 0ffh ;273a
+ db 0cfh ;273b
+ db 000h ;273c
+ dw x280e
+
+ db 0c7h ;273f
+ db 0c7h ;2740
+ db 000h ;2741
+ dw x27ea
+
+ db 0c7h ;2744
+ db 0c4h ;2745
+ db 000h ;2746
+ dw x275e
+
+ db 0f7h ;2749
+ db 010h ;274a
+ db 000h ;274b
+ dw x2775
+
+ db 0e7h ;274e
+ db 020h ;274f
+ db 000h ;2750
+ dw x2775
+
+ db 0c7h ;2753
+ db 0c2h ;2754
+ db 000h ;2755
+ dw x2769
+
+ db 0c7h ;2758
+ db 0c0h ;2759
+ db 000h ;275a
+ dw x27b3
+
+ db 000h ;275d
+
+x275e:
+ ld a,(XBFE8) ;275e
+ and a ;2761
+ jr nz,x2769 ;2762
+ ld a,(TCFLG) ;2764
+ and a ;2767
+ ret nz ;2768
+
+x2769:
+ comst ;2769
+ ld l,(iy+001h) ;276d
+ ld h,(iy+002h) ;2770
+ comend
+ scf ;2773
+ ret ;2774
+
+x2775:
+ comst ;2775
+ ld c,(iy+001h) ;2779
+ comend
+ ld a,c ;277c
+ rla ;277d
+ sbc a,a ;277e
+ ld b,a ;277f
+ ld hl,(REG.PC) ;2780
+ add hl,bc ;2783
+ inc hl ;2784
+ inc hl ;2785
+ scf ;2786
+ ret ;2787
+
+x2788:
+ ld hl,(REG.L) ;2788
+ scf ;278b
+ ret ;278c
+
+x278d:
+ ld hl,(reg.ix) ;278d
+ jr l2795h ;2790
+
+x2792:
+ ld hl,(reg.iy) ;2792
+l2795h:
+ comst ;2795
+ ld a,(iy+001h) ;2799
+ comend
+ cp 0e9h ;279c
+ scf ;279e
+ ret z ;279f
+ and a ;27a0
+ ret ;27a1
+
+x27a2:
+ comst ;27a2
+ ld a,(iy+001h) ;27a6
+ comend
+ cp 04dh ;27a9
+ jr z,x27c9 ;27ab
+ cp 045h ;27ad
+ jr z,x27c9 ;27af
+ and a ;27b1
+ ret ;27b2
+
+x27b3:
+ comst ;27b3
+ ld a,(iy+000h) ;27b7
+ comend
+ ld (XBEDD),a ;27ba
+ ld hl,(REG.F) ;27bd
+ push hl ;27c0
+ pop af ;27c1
+ call XBEDD ;27c2
+ scf ;27c5
+ jr c,x27c9 ;27c6
+ ret ;27c8
+
+x27c9:
+ ld a,(XBFE8) ;27c9
+ and a ;27cc
+ jr nz,l27dah ;27cd
+ ld a,(TCFLG) ;27cf
+ and a ;27d2
+ jr z,l27dah ;27d3
+ call l27dah ;27d5
+ pop hl ;27d8
+ ret ;27d9
+l27dah:
+ ld hl,(REG.SP) ;27da
+ comst ;27dd
+ ld e,(hl) ;27e1
+ inc hl ;27e2
+ ld d,(hl) ;27e3
+ comend
+ ex de,hl ;27e4
+ call sub_1117h ;27e5
+ and a ;27e8
+ ret ;27e9
+
+x27ea:
+ ld a,(ddtrst) ;27ea
+ comst ;27ed
+ cp (iy+000h) ;27f1
+ comend
+ ret z ;27f4
+ comst ;27f5
+ ld a,(iy+000h) ;27f9
+ comend
+ and 038h ;27fc
+ ld l,a ;27fe
+ ld h,000h ;27ff
+ ld a,(XBFE8) ;2801
+ and a ;2804
+ jr nz,l280ch ;2805
+ ld a,(TCFLG) ;2807
+ and a ;280a
+ ret nz ;280b
+l280ch:
+ scf ;280c
+ ret ;280d
+
+x280e:
+ and a ;280e
+ ret ;280f
+
+CMD.C:
+ ld hl,CMD.C ;2810
+ ld a,001h ;2813
+ jr l281bh ;2815
+
+CMD.T:
+ xor a ;2817
+ ld hl,CMD.T ;2818
+l281bh:
+ ld (CMD_RPT),hl ;281b
+ ld (TCFLG),a ;281e
+ ld a,(de) ;2821
+ sub 'N' ;2822
+ jr nz,l2827h ;2824
+ inc de ;2826
+l2827h:
+ ld (TCNFLG),a ;2827
+ ld a,(de) ;282a
+ sub 'J' ;282b
+ jr nz,l2830h ;282d
+ inc de ;282f
+l2830h:
+ ld (TRJFLG),a ;2830
+ call sub_289fh ;2833
+ jr z,l283eh ;2836
+ ld hl,1 ;2838
+ call get_lastarg_def ;283b
+l283eh:
+ ld (TCCSTR),hl ;283e
+ sub a ;2841
+ ld (XA747),a ;2842
+l2845h:
+ call sub_26e7h ;2845
+ jr l289ch ;2848
+l284ah:
+ call sub_0e68h ;284a
+ ld a,(TRJFLG) ;284d
+ and a ;2850
+ jr nz,l2864h ;2851
+ ld iy,(REG.PC) ;2853
+ call sub_28c1h ;2857
+ jr z,l2864h ;285a
+ ld hl,l2726h ;285c
+ call lookup_opc ;285f
+ jr nc,l2845h ;2862
+l2864h:
+ ld a,(XBFEA) ;2864
+ and a ;2867
+ jr z,l2881h ;2868
+ ld de,(TCCSTR) ;286a
+ call EXPR ;286e
+ ld a,h ;2871
+ or l ;2872
+ add a,0ffh ;2873
+ sbc a,a ;2875
+ ld hl,XBFEA ;2876
+ xor (hl) ;2879
+ bit 1,a ;287a
+ jr z,l288ch ;287c
+l287eh:
+ jp l102eh ;287e
+l2881h:
+ ld hl,(TCCSTR) ;2881
+ dec hl ;2884
+ ld (TCCSTR),hl ;2885
+ ld a,h ;2888
+ or l ;2889
+ jr z,l287eh ;288a
+l288ch:
+ call sub_26e7h ;288c
+ jr nc,l287eh ;288f
+ ld a,(TCNFLG) ;2891
+ ld b,a ;2894
+ ld a,(XA747) ;2895
+ or b ;2898
+ ld (XA747),a ;2899
+l289ch:
+ jp l1183h ;289c
+
+sub_289fh:
+ call SKIPBL ;289f
+ xor a ;28a2
+ ld (XBFEA),a ;28a3
+ ld a,(de) ;28a6
+ cp 'U' ;28a7
+ jr z,l28aeh ;28a9
+ cp 'W' ;28ab
+ ret nz ;28ad
+
+l28aeh:
+ inc de ;28ae
+ push af ;28af
+ push de ;28b0
+ call EXPR ;28b1
+ jp c,ERROR ;28b4
+ call assert_eol ;28b7
+ pop hl ;28ba
+ pop af ;28bb
+ ld (XBFEA),a ;28bc
+ sub a ;28bf
+ ret ;28c0
+
+sub_28c1h:
+ comst
+ ld a,(iy+000h)
+ ld b,(iy+0001)
+ comend
+ cp 0edh
+ jr z,l28dbh
+ and 0dfh
+ cp 0ddh
+ ret nz
+ ld a,b
+ cp 0e9h
+ ret
+l28dbh:
+ ld a,b
+ and 0f7h
+ cp 045h
+ ret
+
+?excom:
+ ex (sp),hl ;28e7
+ push af ;28e8
+ push bc ;28e9
+ push de ;28ea
+ ld c,(hl) ;28eb
+ ld b,000h ;28ec
+ inc hl ;28ee
+ ld a,?lcmax
+ sub c
+ ld de,?exeit ;28ef
+ ldir
+ ex de,hl
+ ld (hl),018h
+ inc hl
+ ld (hl),a
+ ex de,hl
+ pop de ;28f7
+ pop bc ;28f8
+ pop af ;28f9
+ ex (sp),hl ;28fa
+?exclst:
+ push hl ;28fb
+ ld hl,(ubbr) ;2900
+
+ if ROMSYS
+ push af ;28fc
+ ld a,(uromen) ;28fd
+ endif
+ jp ?comcod ;2903
+
+;------------------------------------------
+; ddtram
+;------------------------------------------
+
+vartab:
+ dseg
+ddtram:
+;todo:
+; The following 2 params are changeable by user.
+; Should these moved to top ram?
+;
+ddtrst: inidat ;
+ rst DRSTNUM ;rst used by ddtz
+ inidate ;
+ddtei: inidat ;
+ ei ;ints enabled/disabled while ddtz is running
+ ret ;
+ inidate ;
+offs.pc:
+ inidat
+ dw TPA
+ inidate
+offs.@:
+ inidat
+ dw 0
+ inidate
+CMD_ERR:
+ inidat
+ dw 0
+ inidate
+CMD_RPT:
+ inidat
+ dw DDTZML
+ inidate
+ci.buf:
+ inidat
+ db 80
+ rept 83
+ db 0
+ endm
+ inidate
+CON.COL:
+ inidat
+ db 0
+ inidate
+XA747:
+ inidat
+ db 0
+ inidate
+bp_tab:
+ inidat
+ rept BP_CNT
+ db 0,0
+ dw 0,0,0
+ endm
+BP_SIZE equ 8
+ inidate
+sexp1:
+ inidat
+ dw sexpbuf
+ inidate
+sexp2:
+ inidat
+ dw sexpbuf
+ inidate
+sexpbuf:
+ inidat
+ rept 128
+ db 0
+ endm
+ inidate
+sexpbufe:
+
+msg.Y:
+ inidat
+ dc 'Y0'
+ inidate
+reg.Y:
+ inidat
+ rept 10
+ dw 0
+ endm
+ inidate
+lst.S:
+ inidat
+ dw 0
+ inidate
+lst.IP:
+ inidat
+ dw 0
+ inidate
+lst.OP:
+ inidat
+ dw 0
+ inidate
+lst.OD:
+ inidat
+ db 0
+ inidate
+lst.Qj:
+ inidat
+ db 0
+ inidate
+lst.D:
+ inidat
+ dw 0
+ inidate
+HILOD:
+ inidat
+ dw 0
+ inidate
+MAXLOD:
+ inidat
+ dw 0
+ inidate
+XB068:
+ inidat
+ dw 0
+ inidate
+lst.A:
+ inidat
+ dw 0
+ inidate
+XB06C:
+ inidat
+ dw 0
+ inidate
+pfx.IXY:
+ inidat
+ db 000h
+ inidate
+is.pfx.IXY:
+ inidat
+ db 000h
+ inidate
+lst.L:
+ inidat
+ dw 0
+ inidate
+XBE01:
+ inidat
+ dw 0
+ inidate
+XBE03:
+ inidat
+ db 000h
+ inidate
+XBEDD:
+ inidat
+ ret ;ret cc
+ and a
+ pop hl
+ inc hl
+ jp (hl)
+ inidate
+XBFE8:
+ inidat
+ db 0
+ inidate
+TCFLG:
+ inidat
+ db 0
+ inidate
+XBFEA:
+ inidat
+ db 0
+ inidate
+TCCSTR:
+ inidat
+ dw 0
+ inidate
+TCNFLG:
+ inidat
+ db 0
+ inidate
+TRJFLG:
+ inidat
+ db 0
+ inidate
+wstrtflg:
+ inidat
+ db 1
+ inidate
+
+ cseg
+vartabe:
+
+;------------------------------------------
+
+ .phase sysram_start+stacksize
+$stack:
+$stcka equ $ - stacksize
+
+curphse defl $
+ .dephase
+sysramc:
+ .phase curphse
+topcodbeg:
+
+reg.l2: db 0 ; 0fe50h
+reg.h2: db 0 ; 0fe51h
+reg.e2: db 0 ; 0fe52h
+reg.d2: db 0 ; 0fe53h
+reg.c2: db 0 ; 0fe54h
+reg.b2: db 0 ; 0fe55h
+reg.f2: db 0 ; 0fe56h
+reg.a2: db 0 ; 0fe57h
+ db 0
+reg.i: db high ivtab
+reg.iy: dw 0 ; 0fe5ah
+reg.ix: dw 0 ; 0fe5ch
+reg.e: db 0 ; 0fe5eh
+reg.d: db 0 ; 0fe5fh
+reg.c: db 0 ; 0fe60h
+reg.b: db 0 ; 0fe61h
+ if ROMSYS
+udcntl: db CWAITIO ; 0fe62h (mem-, io- wait)
+uromen: db ROM_DIS ; 0fe63h
+ endif
+ubbr: db 0 ; 0fe64h
+ucbar: db USR$CBAR ; 0fe65h
+reg.f: db 0 ; 0fe66h
+reg.a: db 0 ; 0fe67h
+reg.l: db 0 ; 0fe68h
+reg.h: db 0 ; 0fe69h
+reg.sp: dw TOPRAM ; 0fe6ah
+
+$go:
+ if ROMSYS
+ out (000h),a ;064c fe6c
+ out0 (dcntl),l ;064e
+ pop hl ;0651
+ endif
+ out0 (cbar),h ;0652
+ out0 (bbr),l ;0655
+ pop af ;0658
+ pop hl ;0659
+ ld sp,(reg.sp) ;065a
+reg.iff:
+ ei ;065e
+ db 0C3h ;jp TPA ;065f feff ($+1): reg.pc
+reg.pc:
+ dw TPA
+
+bpent:
+ ld (reg.l),hl ;0662 fe82: bpent:
+ pop hl ;0665
+ dec hl ;0666
+ ld (reg.pc),hl ;0667
+ ld (reg.sp),sp ;066a
+ ld sp,reg.l ;066e
+ push af ;0671
+ in0 h,(cbar) ;0672
+ in0 l,(bbr) ;0675
+ push hl ;0678
+ ld a,SYS$CBAR ;0679
+ out0 (cbar),a ;067b
+
+ if ROMSYS
+ in0 l,(dcntl) ;067e
+ ld a,CWAITROM+CWAITIO ;0681
+ out0 (dcntl),a ;0683
+ ld a,($crom) ;0686
+ cp c$rom ;0689
+ ld a,ROM_EN ;068b
+ out (000h),a ;068d
+ endif
+
+ jp bpddtz ;068f
+
+?comcod:
+ if ROMSYS
+ out (000h),a ;0692 feb2
+ pop af ;069a
+ endif
+
+ out0 (cbar),h ;0694
+ out0 (bbr),l ;0697
+ pop hl ;069b
+?exeit:
+ ds ?lcmax+2
+ push af ;069f
+ ld a,SYS$CBAR ;06a0
+ out0 (cbar),a ;06a2
+
+ if ROMSYS
+ ld a,ROM_EN ;06a5
+ out (000h),a ;06a7
+ endif
+
+ pop af ;06a9
+ ret ;06aa
+
+topcodend:
+curph defl $
+ .dephase
+sysrame:
+
+ end
+
diff --git a/z180/fifoio.180 b/z180/fifoio.180
new file mode 100644
index 0000000..27745d4
--- /dev/null
+++ b/z180/fifoio.180
@@ -0,0 +1,128 @@
+ page 255
+ .z80
+
+ global f.init,f.in,f.out,f.i.st
+
+ extrn buf.init
+
+ include config.inc
+ include z180reg.inc
+
+
+;--------------------------------------------------------------
+
+ dseg
+
+
+ mkbuf rx.buf,rx.buf_len
+ mkbuf tx.buf,tx.buf_len
+
+
+;--------------------------------------------------------------
+
+ cseg
+;
+; FIFO channels for communication with stm32
+;
+; Init Serial I/O for console input and output
+;
+
+f.init:
+ ld ix,rx.buf
+ ld a,rx.buf.mask
+ call buf.init
+ ld ix,tx.buf
+ ld a,tx.buf.mask
+ jp buf.init
+
+
+f.i.st:
+ push ix
+ ld ix,rx.buf ;
+
+buf.empty:
+ ld a,(ix+o.in_idx) ;
+ sub (ix+o.out_idx) ;
+ pop ix
+ ret z
+ or 0ffh
+ ret
+
+
+f.in:
+ push ix
+ ld ix,rx.buf ;
+
+buf.get:
+ ld a,(ix+o.out_idx) ;
+bg.wait:
+ cp (ix+o.in_idx) ;
+ jr z,bg.wait
+
+ push hl ;
+ push ix
+ pop hl
+ add a,l
+ ld l,a
+ jr nc,bg.nc
+ inc h
+bg.nc:
+ ld l,(hl)
+
+ ld a,(ix+o.out_idx) ;
+ inc a
+ and (ix+o.mask)
+ ld (ix+o.out_idx),a
+
+ ld a,l
+ pop hl
+ pop ix
+ ret
+
+
+f.o.st:
+ push ix
+ ld ix,tx.buf ;
+
+buf.full:
+ ld a,(ix+o.in_idx) ;
+ inc a
+ and (ix+o.mask)
+ sub (ix+o.out_idx) ;
+ pop ix
+ ret z
+ or 0ffh
+ ret
+
+
+f.out:
+ push ix
+ ld ix,tx.buf ;
+
+buf.put:
+ push hl ;
+ push bc
+ push ix
+ pop hl
+ ld c,(ix+o.in_idx) ;
+ ld b,0
+ add hl,bc
+ ld b,a
+
+ ld a,c ;
+ inc a
+ and (ix+o.mask)
+bp.wait:
+ cp (ix+o.out_idx) ;
+ jr z,bp.wait
+ ld (hl),b
+ ld (ix+o.in_idx),a
+
+ ld a,b
+ pop bc
+ pop hl
+ pop ix
+ ret
+
+ end
+
diff --git a/z180/msgbuf.180 b/z180/msgbuf.180
new file mode 100644
index 0000000..2c67527
--- /dev/null
+++ b/z180/msgbuf.180
@@ -0,0 +1,129 @@
+ page 255
+ .z80
+
+ global msg_fifo
+ global msginit
+ global msg.out,msg.sout,msg.co
+
+ extrn buf.init
+
+ include config.inc
+ include z180reg.inc
+
+;--------------------------------------------------------------
+
+ dseg
+
+ mkbuf msg_fifo, 0
+
+
+;--------------------------------------------------------------
+
+ cseg
+
+;
+; Init buffer
+;
+
+msginit:
+ ld ix,msg_fifo
+ ld a,msg_fb_len-1
+ jp buf.init
+
+
+;--------------------------------------------------------------
+
+msg.sts:
+ push ix
+ ld ix,msg_fifo ;
+
+ ld a,(ix+o.in_idx) ;
+ inc a
+ and (ix+o.mask)
+ sub (ix+o.out_idx) ;
+ pop ix
+ ret z
+ or 0ffh
+ ret
+
+;--------------------------------------------------------------
+
+msg.out:
+ push ix
+ ld ix,msg_fifo ;
+
+ push bc
+ ld b,a ;save char
+ ld a,(ix+o.in_idx) ;
+ inc a
+ and (ix+o.mask)
+bp.wait:
+ cp (ix+o.out_idx) ;
+ jr z,bp.wait
+ ld c,a
+ ld a,b
+ out (PMSG),a
+ ld (ix+o.in_idx),c
+
+ pop bc
+ pop ix
+ ret
+
+;--------------------------------------------------------------
+;
+; (hl): data
+
+msg.sout:
+ push ix
+ ld ix,msg_fifo ;
+ push bc
+
+ ld b,(hl) ;count
+ inc hl
+obs_1:
+ ld a,(ix+o.out_idx) ;
+ sub (ix+o.in_idx) ;
+ dec a
+ and (ix+o.mask)
+ cp b
+ jr c,obs_1
+
+ ld c,(hl) ;port address
+ inc hl
+ ld a,b
+ otir
+ add (ix+o.in_idx)
+ and (ix+o.mask)
+ ld (ix+o.in_idx),a
+ pop bc
+ pop ix
+ ret
+
+;----------------------------------------------------------------------
+
+msg.co:
+ push af
+ push hl
+ ld (buf_char),a
+ ld hl,buf
+ call msg.sout
+ pop hl
+ pop af
+ ret
+
+
+buf:
+ db buf_end - $ - 2 ;output string length
+ db PMSG ;output port
+ db 081h ; message start token
+ db buf_end - $ - 1 ; message length
+ db 1 ; command
+ db 1 ; subcommand
+buf_char:
+ db 0 ; pay load
+buf_end:
+
+;----------------------------------------------------------------------
+
+ end
+
diff --git a/z180/r3init.180 b/z180/r3init.180
new file mode 100644
index 0000000..7ba9a0c
--- /dev/null
+++ b/z180/r3init.180
@@ -0,0 +1,883 @@
+ page 255
+ .z80
+
+ extrn ddtz,bpent
+ extrn $stack
+ extrn $coninit,$cists,$ci
+
+ extrn romend
+
+
+ global isv_sw
+
+ include config.inc
+ include z180reg.inc
+ include z180.lib
+
+;CR equ 0dh
+
+
+
+;----------------------------------------------------------------------
+
+ cseg
+
+ jp start
+
+; restart vectors
+
+rsti defl 1
+ rept 7
+ db 0, 0, 0, 0, 0
+ jp bpent
+rsti defl rsti+1
+ endm
+
+;----------------------------------------------------------------------
+
+ if ROMSYS
+$crom: defb c$rom ;
+ else
+ db 0 ;
+ endif
+
+dmclrt: ;clear ram per dma
+ db dmct_e-dmclrt-2 ;
+ db sar0l ;first port
+ dw nullbyte ;src (fixed)
+nullbyte:
+ db 000h ;src
+ dw romend ;dst (inc), start after "rom" code
+ db 00h ;dst
+ dw 0-romend ;count (64k)
+dmct_e:
+
+INIWAITS defl CWAITIO
+ if ROMSYS
+INIWAITS defl INIWAITS+CWAITROM
+ endif
+
+hwini0:
+ db 3 ;count
+ db rcr,CREFSH ;configure DRAM refresh
+ db dcntl,INIWAITS ;wait states
+ db cbar,SYS$CBAR
+
+;----------------------------------------------------------------------
+
+start:
+ push af ;003c
+ in0 a,(itc) ;003d Illegal opcode trap?
+ jp p,??st01 ;0040
+ pop af ;0043
+ jp bpent ;0044 yes, handle
+
+??st01:
+ ld a,i ;0047 I register == 0 ?
+ jr z,??st02 ;004b yes, harware reset
+ pop af ;004d
+ jp bpent ;004e no, allready set up
+
+??st02:
+ di ;0058
+ ld a,CREFSH
+ out0 (rcr),a ; configure DRAM refresh
+ ld a,CWAITIO
+ out0 (dcntl),a ; wait states
+
+; search warm start mark
+
+ ld ix,mark_55AA ;00b8 ; top of common area
+ ld a,SYS$CBAR ;
+ out0 (cbar),a ;
+ ld a,071h ;00bc
+ ex af,af' ;00be ;for cbr = 0x70 downto 0x40
+swsm_l:
+ ex af,af' ;00bf
+ dec a ;00c0
+ cp 03fh ;00c1
+ jr z,kstart ;00c3 ; break (mark not found)
+ out0 (cbr),a ;00c5
+ ex af,af' ;00c8
+ ld a,0aah ;00c9
+ cp (ix+000h) ;00cb
+ jr nz,swsm_l ;00ce
+ cp (ix+002h) ;00d0
+ jr nz,swsm_l ;00d3
+ cpl ;00d5
+ cp (ix+001h) ;00d6
+ jr nz,swsm_l ;00d9
+ cp (ix+003h) ;00db
+ jr nz,swsm_l ;00de
+ ld sp,$stack ;00e0 mark found, check
+ call checkcrc_alv ;00e3
+ jp z,wstart ;00e6 check ok,
+
+;
+; ram not ok, initialize -- kstart --
+
+kstart:
+
+ ld a,088h ;00e9 0000-7fff: common 0
+ out0 (cbar),a ;00eb 8000-ffff: common 1
+ ld ix,08000h ;00f3
+ ld a,0 ;00f1 start at 008000 (2. phys. 32k block)
+??f_0:
+ out0 (cbr),a ;00f9
+
+ ld (ix+0),a ;0103
+ cpl
+ ld (ix+1),a ;0103
+ cpl
+ add a,8 ;010a next 'bank'
+ cp 078h ;010c stop at 078000
+ jr nz,??f_0 ;010e
+
+ ld de,8000h ;0114 first block not tested, but mark as ok
+ ld a,0 ;00f1 start at 008000 (2. phys. 32k block)
+??cp_0:
+ out0 (cbr),a ;011c
+ ld c,a
+ xor (ix+0)
+ ld b,a
+ ld a,c
+ cpl
+ xor (ix+1)
+ or b
+ jr nz,??cp_1
+ scf
+??cp_1:
+ rr d
+ rr e
+ ld a,c
+ add a,8
+ cp 078h ; stop at 078000
+ jr nz,??cp_0
+
+;
+; ram test found 1 or more error free blocks (32k)
+;
+
+ramok:
+ ld a,SYS$CBAR ;01c8
+ out0 (cbar),a ;01ca
+ ld h,d
+ ld l,e
+ ld c,070h ;01ce highest block
+ ld b,15 ;01d0
+??sr_1:
+ add hl,hl
+ jr c,alloc ;01d4 highest "error free" block
+ ld a,c ;01d6
+ sub 008h ;01d7
+ ld c,a ;01d9
+ djnz ??sr_1 ;01da
+
+ slp ;01dc should never be reached
+
+alloc:
+ out0 (cbr),c ;01de
+ ld sp,$stack ;01e1
+
+; Clear RAM using DMA0
+
+ ld hl,dmclrt ;load DMA registers
+ call io.ini.m
+ ld a,0cbh ;01ef dst +1, src fixed, burst
+ out0 (dmode),a ;01f1
+
+ ld b,512/64
+ ld a,062h ;01f4 enable dma0,
+??cl_1:
+ out0 (dstat),a ;01f9 clear (up to) 64k
+ djnz ??cl_1 ; end of RAM?
+
+; Init bank manager
+
+ ld hl,banktabsys ;020f
+ ld (hl),c ; Common area
+ inc hl ;0213
+ ld (hl),c ; System work area
+ inc hl ;0215 Point to bank 0 entry
+ ld b,BANKS ;0216
+l0218h:
+ ld (hl),0ffh ;0218 Mark all banks as unassigned
+ inc hl ;021a
+ djnz l0218h ;021b
+
+ ld hl,memalv ;
+ ld b,8 ; 8*4k ie. first 32k
+??a_0:
+ ld (hl),0e0h ; mark as sys ("rom"/monitor)
+ inc hl
+ djnz ??a_0
+
+ rr d ; shift out bit for block 0
+ rr e ;
+ ld c,15 ;022c 15*32k remaining blocks
+l022eh:
+ ld a,0feh ; 0xfe == block with error(s)
+ rr d ;
+ rr e
+ adc a,0 ; ==> 0xff : block ok
+ ld b,32/4 ; 32k == 8 * 4k
+l0236h:
+ ld (hl),a ;
+ inc hl ;
+ djnz l0236h ;
+ dec c ;
+ jr nz,l022eh ;next 32k block
+
+ ld hl,memalv+0ch ;memalv+0ch
+ ld a,(banktabsys) ;
+ call add_hl_a
+ ld b,3 ;
+l024ah:
+ ld (hl),0ech ;alloc system ram
+ inc hl ;
+ djnz l024ah ;
+ ld (hl),0efh ;alloc common
+ call gencrc_alv
+
+ ld hl,0000h ;bank #
+ ld bc,0f0fh ; size (?) (4k blocks)
+ xor a ;
+ call sub_0420h ;alloc mem for bank 0
+ ld c,l ;
+ or a ;
+ call z,sub_04b5h ;
+
+ ld hl,0101h ;
+ ld bc,0f0fh ;
+ xor a ;
+ call sub_0420h ;
+ ld c,l ;
+ or a ;
+ call z,sub_04b5h ;
+
+ ld hl,055AAh ;set warm start mark
+ ld (mark_55AA),hl ;
+ ld (mark_55AA+2),hl;
+
+;
+; crc ok -- wstart --
+;
+wstart:
+ call sysram_init ;027f
+ call ivtab_init
+
+ call prt0_init
+
+
+;;; call bufferinit
+
+
+ call $coninit
+
+
+
+
+ im 2 ;?030e
+ ei ;0282
+
+ call $cists ;0284
+ call $cists ;0287
+ or a ;028a
+ call nz,$ci ;028d
+
+ ld a,(banktab) ;
+ ld e,a ;
+ jp ddtz ;0290
+
+
+;
+;----------------------------------------------------------------------
+;
+
+;TODO: Make a ringbuffer module.
+
+ global buf.init
+
+buf.init:
+ ld (ix+o.in_idx),0
+ ld (ix+o.out_idx),0
+ ld (ix+o.mask),a
+ ret
+
+;----------------------------------------------------------------------
+
+.comment *
+
+ extrn msginit,msg.sout,msg_fifo
+ extrn tx.buf,rx.buf
+
+
+bufferinit:
+ call msginit
+
+ ld hl,buffers
+ ld bc,0300h
+bfi_1:
+ ld e,(hl)
+ inc hl
+ ld d,(hl)
+ inc hl
+ push hl
+ in0 a,cbr
+ call log2phys
+ ld (bufdat+1),hl
+ ld (bufdat+3),a
+ ld a,c
+ ld (bufdat+0),a
+ ld hl,inimsg
+ call msg.sout
+ pop hl
+ inc c
+ djnz bfi_1
+ ret
+
+ rept 20
+ db 0
+ endm
+
+buffers:
+ dw msg_fifo
+ dw tx.buf
+ dw rx.buf
+
+inimsg:
+ db inimsg_e - $ -2
+ db PMSG
+ db 81h
+ db inimsg_e - $ -1
+ db 0
+bufdat:
+ db 0
+ dw 0
+ db 0
+inimsg_e:
+
+ *
+
+;
+;----------------------------------------------------------------------
+;
+
+sysram_init:
+ ld hl,sysramw
+ ld de,topcodsys
+ ld bc,sysrame-sysramw
+ ldir
+
+ ret
+
+;----------------------------------------------------------------------
+
+ivtab_init:
+ ld hl,ivtab ;
+ ld a,h ;
+ ld i,a ;
+ out0 (il),l ;
+
+; Let all vectors point to spurious int routines.
+
+ ld d,high sp.int0
+ ld a,low sp.int0
+ ld b,9
+ivt_i1:
+ ld (hl),a
+ inc l
+ ld (hl),d
+ inc l
+ add a,sp.int.len
+ djnz ivt_i1
+ ret
+
+;----------------------------------------------------------------------
+
+prt0_init:
+ ld a,i
+ ld h,a
+ in0 a,(il)
+ and 0E0h
+ or IV$PRT0
+ ld l,a
+ ld (hl),low iprt0
+ inc hl
+ ld (hl),high iprt0
+ ld hl,prt0itab
+ call io.ini.m
+ ret
+
+prt0itab:
+ db prt0it_e-prt0itab-2
+ db tmdr0l
+ dw PRT_TC10MS
+ dw PRT_TC10MS
+ db M_TIE0+M_TDE0 ;enable timer 0 interrupt and down count.
+prt0it_e:
+
+
+;
+;----------------------------------------------------------------------
+;
+
+io.ini:
+ push bc
+ ld b,0 ;high byte port adress
+ ld a,(hl) ;count
+ inc hl
+ioi_1:
+ ld c,(hl) ;port address
+ inc hl
+ outi
+ inc b ;outi decrements b
+ dec a
+ jr nz,ioi_1
+ pop bc
+ ret
+
+io.ini.m:
+ push bc
+ ld b,(hl)
+ inc hl
+ ld c,(hl)
+ inc hl
+ otimr
+ pop bc
+ ret
+
+io.ini.l:
+;
+
+;----------------------------------------------------------------------
+;
+
+; compute crc
+; hl: start adr
+; bc: len
+; bc returns crc val
+
+do_crc16:
+ ld de,0FFFFh
+crc1:
+ ld a,(hl)
+ xor e
+ ld e,a
+ rrca
+ rrca
+ rrca
+ rrca
+ and 0Fh
+ xor e
+ ld e,a
+ rrca
+ rrca
+ rrca
+ push af
+ and 1Fh
+ xor d
+ ld d,a
+ pop af
+ push af
+ rrca
+ and 0F0h
+ xor d
+ ld d,a
+ pop af
+ and 0E0h
+ xor e
+ ld e,d
+ ld d,a
+ cpi
+ jp pe,crc1
+ or e ;z-flag
+ ret
+
+
+gencrc_alv:
+ push hl ;03f6
+ push de ;03f7
+ push bc
+ push af ;03f8
+ ld hl,banktabsys ;03f9
+ ld bc,crc_len ;03fc
+ call do_crc16 ;03ff
+ ld (hl),e
+ inc hl
+ ld (hl),d
+ pop af ;0406
+ pop bc
+ pop de ;0407
+ pop hl ;0408
+ ret ;0409
+
+checkcrc_alv:
+ push hl ;040a
+ push de
+ push bc ;040b
+ ld hl,banktabsys ;040d
+ ld bc,crc_len+2 ;0410
+ call do_crc16 ;0413
+ pop bc ;041d
+ pop de
+ pop hl ;041e
+ ret ;041f
+
+;----------------------------------------------------------------------
+
+;
+; alloc
+;
+; h: max bank #
+; l: min bank #
+; b: max size
+; c: min size
+;
+; ret:
+; a: 0 == ok
+; 1 ==
+; 2 == no bank # in requested range
+; ff == crc error
+;
+
+sub_0420h:
+ call checkcrc_alv ;0420
+ jr nz,l049ch ;0424 crc error, tables corrupt
+
+ call sub_049dh ;0427 bank # in req. range available?
+ jr c,l0499h ;042a
+ push ix ;042c
+ push iy ;042e
+ push de ;0430
+ push hl ;0431
+ push bc ;0432
+ ld c,b ;0433
+ ld b,alv_len+1 ;0434
+ ld d,0 ;0436
+ ld hl,memalv-1 ;0438
+ jr l0441h ;043b
+
+; find free blocks
+
+l043dh:
+ ld a,(hl) ;043d
+ inc a ;043e free blocks are marked 0ffh
+ jr z,l0446h ;043f
+l0441h:
+ inc hl ;0441
+ djnz l043dh ;0442
+ jr l0464h ;0444
+l0446h:
+ push hl ;0446
+ pop ix ;0447 free blocks start here
+ ld e,000h ;0449
+ jr l0451h ;044b
+l044dh: ; count free blocks
+ ld a,(hl) ;044d
+ inc a ;044e
+ jr nz,l0457h ;044f
+l0451h:
+ inc e ;0451
+ inc hl ;0452
+ djnz l044dh ;0453
+ jr l0464h ;0455
+
+; end of free blocks run.
+
+l0457h:
+ ld a,d ;0457
+ cp e ;0458 nr of blocks >= requested ?
+ jr nc,l0441h ;0459
+
+ ld d,e ;045b
+ push ix ;045c
+ pop iy ;045e
+ ld a,d ;0460
+ cp c ;0461
+ jr c,l0441h ;0462
+l0464h:
+ pop bc ;0464
+ ld a,d ;0465
+ cp b ;0466
+ jr c,l046ch ;0467
+ ld d,b ;0469
+ jr l0471h ;046a
+l046ch:
+ cp c ;046c
+ jr nc,l0471h ;046d
+ ld d,000h ;046f
+l0471h:
+ ld a,d ;0471
+ push iy ;0472
+ pop hl ;0474
+ ld de,memalv ;0475
+ or a ;0478
+ sbc hl,de ;0479
+ ld b,l ;047b
+ ld c,a ;047c
+ pop hl ;047d
+l047eh:
+ or a ;047e
+ jr z,l0489h ;047f
+ ld (iy+0),l ;0481
+ inc iy ;0484
+ dec a ;0486
+ jr l047eh ;0487
+l0489h:
+ pop de ;0489
+ pop iy ;048a
+ pop ix ;048c
+ call gencrc_alv ;048e
+ ld a,c ;0491
+ or a ;0492
+ ld a,000h ;0493
+ ret nz ;0495
+ or 001h ;0496
+ ret ;0498
+
+l0499h:
+ ld a,2 ;0499
+l049ch:
+ or a
+ ret ;049c
+
+
+; search a free bank number in range
+; h: max #
+; l: min #
+; ret:
+; l: bank number available
+; nc, if found, bank nr. in l
+; cy, if none found
+
+sub_049dh:
+ push de ;049d
+ push bc ;049e
+ ex de,hl ;049f
+ dec e ;04a0
+l04a1h:
+ inc e ;04a1 test next #
+ ld a,d ;04a2
+ cp e ;04a3
+ jr c,l04b1h ;04a4
+ ld a,e ;04a6
+ ld hl,memalv ;04a7
+ ld bc,alv_len ;04aa
+ cpir ;04ad bank# allready allocated?
+ jr z,l04a1h ;04af if yes, search for next
+l04b1h:
+ ex de,hl ;04b1
+ pop bc ;04b2
+ pop de ;04b3
+ ret ;04b4
+
+
+sub_04b5h:
+ ld a,l ;04b5
+ cp 012h ;04b6
+ ccf ;04b8
+ ret c ;04b9
+ push hl ;04ba
+ ld hl,banktab ;04bb
+ call add_hl_a
+ ld (hl),b ;04c3
+ call gencrc_alv ;04c4
+ pop hl ;04c7
+ or a ;04c8 clear carry
+ ret ;04c9
+
+
+;--------------------------------------------------------------
+;
+; de: Log. Address
+; a: Bank number
+;
+;out ahl: Phys. (linear) Address
+
+
+bnk2phys:
+ push hl
+ ld hl,banktab
+ call add_hl_a
+ ld a,(hl)
+ pop hl
+
+ ; fall thru
+;--------------------------------------------------------------
+;
+; de: Log. Address
+; a: Bank (bbr)
+;
+; OP: ahl = (a<<12) + (d<<8) + e
+;
+;out ahl: Phys. (linear) Address
+
+
+log2phys:
+ push bc ;
+ ld c,a ;
+ ld b,16 ;
+ mlt bc ;bc = a<<4
+ ld l,d ;
+ ld h,0 ;
+ add hl,bc ;bc + d == a<<4 + d
+ ld a,h ;
+ ld h,l ;
+ ld l,e ;
+ pop bc ;
+ ret ;
+
+
+;--------------------------------------------------------------
+;
+;return:
+; hl = hl + a
+; Flags undefined
+;
+
+add_hl_a:
+ add a,l
+ ld l,a
+ ret nc
+ inc h
+ ret
+
+; ---------------------------------------------------------
+
+sysramw:
+
+ .phase isvsw_loc
+topcodsys:
+
+; Trampoline for interrupt routines in banked ram.
+; Switch stack pointer to "system" stack in top ram
+; Save cbar
+
+isv_sw: ;
+ ex (sp),hl ; save hl, return adr in hl
+ push de ;
+ push af ;
+ ex de,hl ;
+ ld hl,0 ;
+ add hl,sp ;
+ ld a,h ;
+ cp 0f8h ;
+ jr nc,isw_1 ;
+ ld sp,$stack ;
+isw_1:
+ push hl ;
+ in0 h,(cbar) ;
+ push hl ;
+ ld a,SYS$CBAR ;
+ out0 (cbar),a ;
+ ex de,hl ;
+ ld e,(hl) ;
+ inc hl ;
+ ld d,(hl) ;
+ ex de,hl ;
+ push bc ;
+ call jphl ;
+
+ pop bc ;
+ pop hl ;
+ out0 (cbar),h ;
+ pop hl ;
+ ld sp,hl ;
+ pop af ;
+ pop de ;
+ pop hl ;
+ ei ;
+ ret ;
+jphl:
+ jp (hl) ;
+
+; ---------------------------------------------------------
+
+
+iprt0:
+ push af
+ push hl
+ in0 a,(tcr)
+ in0 a,(tmdr0l)
+ in0 a,(tmdr0h)
+ ld a,(tim_ms)
+ inc a
+ cp 100
+ jr nz,iprt_1
+ xor a
+ ld hl,(tim_s)
+ inc hl
+ ld (tim_s),hl
+iprt_1:
+ ld (tim_ms),a
+ pop hl
+ pop af
+ ei
+ ret
+
+; ---------------------------------------------------------
+
+sp.int0:
+ ld a,0d0h
+ jr sp.i.1
+sp.int.len equ $-sp.int0
+ ld a,0d1h
+ jr sp.i.1
+ ld a,0d2h
+ jr sp.i.1
+ ld a,0d3h
+ jr sp.i.1
+ ld a,0d4h
+ jr sp.i.1
+ ld a,0d5h
+ jr sp.i.1
+ ld a,0d6h
+ jr sp.i.1
+ ld a,0d7h
+ jr sp.i.1
+ ld a,0d8h
+sp.i.1:
+; out (80h),a
+ halt
+
+curph defl $
+ .dephase
+sysrame:
+ .phase curph
+tim_ms: db 0
+tim_s: dw 0
+ .dephase
+
+;-----------------------------------------------------
+
+ dseg
+
+ ds 1
+banktabsys:
+ ds 1 ;0c001h
+ ds 1 ;0c002h
+banktab:
+ ds BANKS ;0c003h
+memalv:
+ ds 512/4 ;Number of 4k blocks
+alv_len equ $-memalv
+crc_len equ $-banktabsys
+
+crc_memalv:
+ ds 2 ;
+
+ cseg
+
+ ;.phase 0ffc0h
+;ivtab equ 0ffc0h ; 0ffc0h ;int vector table
+ ;.dephase
+
+ ;.phase 0fffch
+mark_55AA equ 0fffch
+ ;ds 4 ; 0fffch
+ ;.dephase
+
+
+ end
+
diff --git a/z180/romend.180 b/z180/romend.180
new file mode 100644
index 0000000..2a63342
--- /dev/null
+++ b/z180/romend.180
@@ -0,0 +1,9 @@
+
+ global romend
+
+ cseg
+
+romend equ $
+
+ end
+
diff --git a/z180/ser1-i.180 b/z180/ser1-i.180
new file mode 100644
index 0000000..4074a9d
--- /dev/null
+++ b/z180/ser1-i.180
@@ -0,0 +1,257 @@
+ page 200
+
+
+ extrn buf.init
+ extrn isv_sw
+
+
+ global ser.init
+ global ser.instat,ser.in
+ global ser.out
+
+;TODO: define a trampoline area somewhere in top ram.
+rtxisvjmp equ 0FF60h ;momentan frei...
+
+ include config.inc
+ include z180reg.inc
+
+
+;-----------------------------------------------------
+
+ dseg
+
+buf_start:
+ mkbuf ser1.inbuf,s1.rx_len
+ mkbuf ser1.outbuf,s1.tx_len
+buf_end:
+
+
+
+;-----------------------------------------------------
+
+ cseg
+;
+; Init Serial I/O for console input and output (ASCI1)
+;
+
+
+ser.init:
+; ld a,i
+; push af ;save IFF
+; di
+
+ xor a ;
+ out0 (stat1),a ;Disable rx/tx interrupts
+
+ ld hl,rxtx_src ;move rx and tx isv to common ram
+ ld de,rxtx_dst ;
+ ld bc,rxtx_src_e-rxtx_src ;
+ ldir ;
+
+ ld hl,rtxisvjmp ;rx/tx int vector
+ ld (ivtab + IV$ASCI1),hl;
+ ld a,0cdh ;
+ ld (rtxisvjmp),a ;
+ ld hl,isv_sw ;
+ ld (rtxisvjmp + 1),hl ;
+ ld hl,rxtxisv ;
+ ld (rtxisvjmp + 3),hl ;
+
+; ASCI1: 8N1, highest baudrate (56700), CTS disabled
+
+ ld a,M_MPBT
+ out0 (cntlb1),a
+ ld a,M_RE + M_TE + M_MOD2
+ out0 (cntla1),a
+ ld a,M_RIE
+ out0 (stat1),a ;Enable rx interrupts
+
+ ld ix,ser1.inbuf
+ ld a,ser1.inbuf.mask
+ call buf.init
+ ld ix,ser1.outbuf
+ ld a,ser1.outbuf.mask
+ call buf.init
+
+; pop af
+; ret po
+; ei
+ ret ;
+
+ser.instat:
+ push ix
+ ld ix,ser1.inbuf ;
+
+buf.empty:
+ ld a,(ix+o.in_idx) ;
+ sub (ix+o.out_idx) ;
+ pop ix
+ ret z
+ or 0ffh
+ ret
+
+ser.in:
+ push hl ;11
+ push de ;11
+ ld hl,ser1.inbuf-1 ; 9 hl = &rx.out_idx
+ ld a,(hl) ; 6 a = rx.out_idx
+ dec hl ; 4 hl = &rx.in_idx
+ jr bg.w1
+bg.wait:
+ halt
+bg.w1:
+ cp (hl) ; 6 while (out_idx==in_idx)
+ jr z,bg.wait ; 6 (/8) ;
+
+ ld e,a ; 4
+ ld d,0 ; 6
+ inc de
+ inc de
+
+ ex de,hl ; 3
+ add hl,de ;10
+ ld l,(hl) ; 6
+ ex de,hl ; 3
+
+ inc a ; 4
+ dec hl ; 4
+ and (hl) ; 6
+ inc hl ; 4
+ inc hl ; 4
+ ld (hl),a ; 7
+
+ ld a,e ; 4
+ pop de ; 9
+ pop hl ; 9
+ ret ; 9
+ ; 153
+
+ser.outstat:
+ push ix
+ ld ix,ser1.outbuf ;
+buf.full:
+ ld a,(ix+o.in_idx) ;
+ inc a
+ and (ix+o.mask)
+ sub (ix+o.out_idx) ;
+ pop ix
+ ret z
+ or 0ffh
+ ret
+
+
+ser.out:
+ push ix
+ ld ix,ser1.outbuf ;
+buf.put:
+ push hl ;
+ push bc
+ push ix
+ pop hl
+ ld c,(ix+o.in_idx) ;
+ ld b,0
+ add hl,bc
+ ld (hl),a
+
+ ld a,c ;
+ inc a
+ and (ix+o.mask)
+bp.wait:
+ cp (ix+o.out_idx) ;
+ jr z,bp.wait
+ ld (ix+o.in_idx),a
+
+ di ;036f
+ in0 a,(stat1) ;0374
+ set TIE,a ;0377
+ out0 (stat1),a ;0379
+ ei ;037c
+
+ ld a,b
+ pop bc
+ pop hl
+ pop ix
+ ret
+
+
+;------------------------------------------
+; ASCI 1 Transmit/Receive interupt routines
+; moved to common ram
+
+rxtx_src:
+ dseg
+rxtx_dst: ; (0c097h) old
+
+rxtxisv:
+ inidat
+ in0 a,(stat1) ;receive flag set?
+ jp p,txisv ;
+
+ in0 d,(rdr1) ;todo: break detection
+ bit FE,a ;framing error?
+ jr nz,??ri_1
+
+ push ix
+ ld ix,ser1.inbuf ;
+ ld hl,ser1.inbuf ;
+ ld c,(ix+o.in_idx) ;
+ ld b,0
+ add hl,bc
+
+ ld a,c ;
+ inc a
+ and (ix+o.mask)
+ cp (ix+o.out_idx) ;
+ jr z,??ri_0
+ ld (hl),d
+ ld (ix+o.in_idx),a
+??ri_0:
+ pop ix
+??ri_1:
+ in0 a,(cntla1) ;0705 c0c0
+ res EFR,a ;0708
+ out0 (cntla1),a ;070a
+ ret
+
+ inidate
+
+txisv:
+ inidat
+ push ix
+ ld ix,ser1.outbuf ;
+
+ ld a,(ix+o.out_idx) ;
+ cp (ix+o.in_idx) ;
+ jr z,??ti_2
+
+ ld hl,ser1.outbuf ;
+ add a,l
+ ld l,a
+ jr nc,??ti_1
+ inc h
+??ti_1:
+ ld l,(hl)
+ out0 (tdr1),l ;071b
+
+ ld a,(ix+o.out_idx) ;
+ inc a
+ and (ix+o.mask)
+ ld (ix+o.out_idx),a
+ jr ??ti_3
+??ti_2:
+ in0 a,(stat1) ;0730 disable tx-int
+ res TIE,a ;0733
+ out0 (stat1),a ;0735
+??ti_3:
+ pop ix
+ ret
+
+ inidate
+
+ cseg
+rxtx_src_e:
+
+
+ end
+
+
diff --git a/z180/z180.lib b/z180/z180.lib
new file mode 100644
index 0000000..ffe0ab7
--- /dev/null
+++ b/z180/z180.lib
@@ -0,0 +1,120 @@
+ .xlist
+;;
+;; Z180 / HD64180 MACRO LIBRARY
+;;
+
+IFNDEF SLP
+
+;;
+;;
+;; HD64180 instructions:
+;;
+;; SLP ; Enter SLEEP mode
+;; MLT RR ; 8 bit multiply with 16 bit result
+;; IN0 R,P ; Input from internal port
+;; OUT0 P,R ; Output to internal port
+;; OTIM ; Block output, increment
+;; OTIMR ; Block output, increment and repeat
+;; OTDM ; Block output, decrement
+;; OTDMR ; Block output, decrement and repeat
+;; TSTIO P ; Non destructive AND, I/O port and A
+;; TST R ; Non destructive AND, reg and A
+;; TST ID ; Non destructive AND, immediate data and A
+;; TST (HL) ; Non destructive AND, (HL) and A
+;;
+;;
+
+??BC EQU 0 ; Double-register definitions
+??DE EQU 1
+??HL EQU 2
+??SP EQU 3
+
+??A EQU 7 ; Single-register defintions
+??B EQU 0
+??C EQU 1
+??D EQU 2
+??E EQU 3
+??H EQU 4
+??L EQU 5
+
+SLP MACRO
+ DEFB 0EDH,76H
+ ENDM
+
+MLT MACRO ?R
+ DB 0EDH,4CH+(??&?R AND 3) SHL 4
+ ENDM
+
+IN0 MACRO ?R,?P
+ DB 0EDH,(??&?R AND 7) SHL 3, ?P
+ ENDM
+
+OUT0 MACRO ?P,?R
+ DB 0EDH,1+(??&?R AND 7) SHL 3,?P
+ ENDM
+
+OTIM MACRO
+ DB 0EDH,83H
+ ENDM
+
+OTIMR MACRO
+ DB 0EDH,93H
+ ENDM
+
+OTDM MACRO
+ DB 0EDH,8BH
+ ENDM
+
+OTDMR MACRO
+ DB 0EDH,9BH
+ ENDM
+
+TSTIO MACRO ?P
+ DB 0EDH,74H,?P
+ ENDM
+
+TSTR MACRO ?R
+ DB 0EDH,4+(??&?R AND 7) SHL 3
+ ENDM
+
+TSTD MACRO ?P
+ DB 0EDH,64H,?P
+ ENDM
+
+TSTP MACRO
+ DB 0EDH,34H
+ ENDM
+
+TST MACRO ?R
+ ?D: SET 0
+ IRPC ?X,?R
+ ?D: SET ?D+1 ; Count # chars in "?R"
+ ENDM
+ IF ?D EQ 1 ; IF # chars = 1
+ IRPC ?X,ABCDEHL ; Look up the character
+ IF ??&?X EQ ??&?R
+ DB 0EDH,4+(??&?R AND 7) SHL 3
+ EXITM
+ ENDIF
+ ENDM
+ DB 0EDh,64h,?R
+ EXITM
+ ENDIF
+ IF (?R = 2) & (?D = 4)
+ ?D: SET 0
+ IRPC ?X,?R
+ IFIDN <?X>,<(>
+ DB 0EDh,34h
+ EXITM
+ ENDIF
+ ?D: SET ?D + 1
+ ENDM
+ ENDIF
+ IF ?D NE 0
+ DB 0EDH,64H,?R
+ ENDIF
+ ENDM
+
+ENDIF ;/* IFNDEF SLP /
+ .list
+
diff --git a/z180/z180reg.inc b/z180/z180reg.inc
new file mode 100644
index 0000000..616138c
--- /dev/null
+++ b/z180/z180reg.inc
@@ -0,0 +1,190 @@
+ .xlist
+
+;;
+;; HD64180/Z180 Register Definitions
+;;
+
+
+b2m macro name,nr
+name equ nr
+M_&name equ 1 shl nr
+ endm
+
+; ifndef IOBASE
+IOBASE equ 0
+; endif
+
+cntla0 equ IOBASE+00h ;ASCI Control Register A Channel 0
+cntla1 equ IOBASE+01h ;ASCI Control Register A Channel 1
+ b2m MPE, 7 ;Multi-Processor Mode Enable
+ b2m RE, 6 ;Receiver Enable
+ b2m TE, 5 ;Transmitter Enable
+ b2m RTS0, 4 ;Request to Send Channel 0
+ b2m CKA1D, 4 ;
+ b2m MPBR, 3 ;Multiprocessor Bit Receive (Read)
+ b2m EFR, 3 ;Error Flag Reset (Write)
+ b2m MOD2, 2 ;Data Format Mode 1 = 8-Bit data
+ b2m NOD1, 1 ;1 = Parity enabled
+ b2m MOD0, 0 ;1 = 2 stop bits
+
+cntlb0 equ IOBASE+02h ;ASCI Control Register B Channel 0
+cntlb1 equ IOBASE+03h ;ASCI Control Register B Channel 1
+ b2m MPBT,7 ;Multiprocessor Bit Transmit
+ b2m MP,6 ;Multiprocessor Mode
+ b2m CTS,5 ;Clear to Send
+ b2m PS,5 ;Prescale
+ b2m PEO,4 ;Parity Even Odd
+ b2m DR,3 ;Divede Ratio
+ b2m SS2,2 ;Source/Speed Select 2,1,0
+ b2m SS1,1 ;
+ b2m SS0,0 ;
+
+stat0 equ IOBASE+04h ;ASCI Status Channel 0
+stat1 equ IOBASE+05h ;ASCI Status Channel 1
+ b2m RDRF,7 ;Receive Data Register Full
+ b2m OVRN,6 ;Overrun Error
+ b2m PERR,5 ;Parity Error (M80: PE conflicts with JP/CALL cc)
+ b2m FE,4 ;Framing Error
+ b2m RIE,3 ;Receive Interrupt Enable
+ b2m DCD0,2 ;Data Carrier Detect (Ch 0)
+ b2m CTS1E,2 ;Clear To Send (Ch 1)
+ b2m TDRE,1 ;Transmit Data Register Empty
+ b2m TIE,0 ;Transmit Interrupt Enable
+
+tdr0 equ IOBASE+06h ;ASCI Transmit Data
+tdr1 equ IOBASE+07h ;ASCI Transmit Data
+rdr0 equ IOBASE+08h ;ASCI Receive Data
+rdr1 equ IOBASE+09h ;ASCI Receive Data
+
+cntr equ IOBASE+0Ah ;CSI/O Control Register
+trdr equ IOBASE+0Bh ;CSI/O Transmit/Receive Data Register
+
+tmdr0l equ IOBASE+0Ch ;Timer Data Register Channel 0
+tmdr0h equ IOBASE+0Dh ;
+rldr0l equ IOBASE+0Eh ;Timer Reload Register Channel 0
+rldr0h equ IOBASE+0Fh ;
+tcr equ IOBASE+10h ;Timer Control Register
+ b2m TIF1,7 ;Timer Interrupt Flag
+ b2m TIF0,6 ;
+ b2m TIE1,5 ;Timer Interrupt Enable
+ b2m TIE0,4 ;
+ b2m TOC1,3 ;Timer Output Control
+ b2m TOC0,2 ;
+ b2m TDE1,1 ;Timer Down Count Enable
+ b2m TDE0,0 ;
+
+
+asext0 equ IOBASE+12h ;ASCI Extension Control Register
+asext1 equ IOBASE+13h ;ASCI Extension Control Register
+
+tmdr1l equ IOBASE+14h ;Timer Data Register Channel 1
+tmdr1h equ IOBASE+15h ;
+rldr1l equ IOBASE+16h ;Timer Reload Register Channel 1
+rldr1h equ IOBASE+17h ;
+
+frc equ IOBASE+18h ;Free Running Counter
+
+astc0l equ IOBASE+1Ah ;ASCI Time Constant Register 0
+astc0h equ IOBASE+1Bh ;
+astc1l equ IOBASE+1Ch ;ASCI Time Constant Register 1
+astc1h equ IOBASE+1Dh ;
+
+cmr equ IOBASE+1Eh ;Clock Mutiplier Register
+ b2m X2CM,7 ;X2 Clock Multiplier
+ b2m LNC,6 ;Low Noise Crystal
+
+ccr equ IOBASE+1Fh ;CPU Control Register
+
+sar0l equ IOBASE+20h ;DMA Src Adr Register Channel 0
+sar0h equ IOBASE+21h ;
+sar0b equ IOBASE+22h ;
+dar0l equ IOBASE+23h ;DMA Dst Adr Register Channel 0
+dar0h equ IOBASE+24h ;
+dar0b equ IOBASE+25h ;
+bcr0l equ IOBASE+26h ;DMA Byte Count Register Channel 0
+bcr0h equ IOBASE+27h ;
+
+mar1l equ IOBASE+28h ;DMA Memory Address Register Channel 1
+mar1h equ IOBASE+29h ;
+mar1b equ IOBASE+2Ah ;
+iar1l equ IOBASE+2Bh ;DMA I/O Address Register Channel 1
+iar1h equ IOBASE+2Ch ;
+iar1b equ IOBASE+2Dh ;
+ b2m ALTE,7 ;Alternating Chnnels
+ b2m ALTC,6 ;Currently selected DMA Channel when Bit7=1
+ b2m REQ1SEL2,2 ;
+ b2m REQ1SEL1,1 ;
+ b2m REQ1SEL0,0 ;
+
+bcr1l equ IOBASE+2Eh ;DMA Byte Count Register Channel 1
+bcr1h equ IOBASE+2Fh ;
+
+dstat equ IOBASE+30h ;DMA Status Register
+ b2m DE1,7 ;DMA enable ch 1,0
+ b2m DE0,6 ;
+ b2m DWE1,5 ;DMA Enable Bit Write Enable 1,0
+ b2m DWE0,4 ;
+ b2m DIE1,3 ;DMA Interrupt Enable 1,0
+ b2m DIE0,2 ;
+ b2m DME,0 ;DMA Master enable
+
+dmode equ IOBASE+31h ;DMA Mode Register
+ b2m DM1,5 ;Ch 0 Destination Mode 1,0
+ b2m DM0,4 ;
+ b2m SM1,3 ;Ch 0 Source Mode 1,0
+ b2m SM0,2 ;
+ b2m MMOD,1 ;Memory MODE select (0=cycle steel/1=burst)
+
+dcntl equ IOBASE+32h ;DMA/WAIT Control
+ b2m MWI1,7 ;Memory Wait Insertion
+ b2m MWI0,6 ;
+ b2m IWI1,5 ;I/O Wait Insertion
+ b2m IWI0,4 ;
+ b2m DMS1,3 ;DREQi Select (Edge/Level)
+ b2m DMS0,2 ;
+ b2m DIMA1,1 ;DMA Ch1 I/O Memory Mode Select
+ b2m DIMA0,0
+M_MWI equ M_MWI1 + M_MWI0
+M_IWI equ M_IWI1 + M_IWI0
+
+il equ IOBASE+33h ;Interrupt Vector Low Register
+itc equ IOBASE+34h ;INT/TRAP Control Register
+ b2m TRAP,7 ;Trap
+ b2m UFO,6 ;Unidentified Fetch Object
+ b2m ITE2,2 ;/INT Enable 2,1,0
+ b2m ITE1,1 ;
+ b2m ITE0,0 ;
+
+rcr equ IOBASE+36h ;Refresh Control Register
+ b2m REFE,7 ;Refresh Enable
+ b2m REFW,6 ;Refresh Wait State
+ b2m CYC1,1 ;Cycle select
+ b2m CYC0,0 ;
+
+cbr equ IOBASE+38h ;MMU Common Base Register
+bbr equ IOBASE+39h ;MMU Bank Base Register
+cbar equ IOBASE+3Ah ;MMU Common/Bank Register
+
+omcr equ IOBASE+3Eh ;Operation Mode Control Register
+ b2m M1E,7 ;M1 Enable
+ b2m M1TE,6 ;M1 Temporary Enable
+ b2m IOC,5 ;I/O Compatibility
+
+icr equ IOBASE+3Fh ;I/O Control Register
+ b2m IOSTP,5 ;I/O Stop
+;
+; Interrupt Vectors
+;
+
+IV$INT1 equ 0 ;/INT1 (highest priority)
+IV$INT2 equ 2 ;/INT2
+IV$PRT0 equ 4 ;PRT channel 0
+IV$PRT1 equ 6 ;PRT channel 1
+IV$DMA0 equ 8 ;DMA channel 0
+IV$DMA1 equ 10 ;DMA channel 1
+IV$CSIO equ 12 ;CSI/O
+IV$ASCI0 equ 14 ;ASCI channel 0
+IV$ASCI1 equ 16 ;ASCI channel 1 (lowest priority)
+
+ .list
+