summaryrefslogtreecommitdiff
path: root/avr
diff options
context:
space:
mode:
Diffstat (limited to 'avr')
-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
36 files changed, 6708 insertions, 0 deletions
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);