From 21a24f90c5aaaaf13f91716208b32cde163c5918 Mon Sep 17 00:00:00 2001 From: Leo C Date: Thu, 21 Aug 2014 11:36:14 +0200 Subject: Create include directory --- include/background.h | 10 ++++ include/cli.h | 64 ++++++++++++++++++++ include/cli_readline.h | 50 ++++++++++++++++ include/cmd_mem.h | 30 ++++++++++ include/command.h | 158 +++++++++++++++++++++++++++++++++++++++++++++++++ include/common.h | 61 +++++++++++++++++++ include/con-utils.h | 22 +++++++ include/config.h | 29 +++++++++ include/crc.h | 12 ++++ include/debug.h | 38 ++++++++++++ include/env.h | 10 ++++ include/ring.h | 74 +++++++++++++++++++++++ include/serial.h | 9 +++ include/timer.h | 7 +++ include/xmalloc.h | 8 +++ include/z80-if.h | 46 ++++++++++++++ 16 files changed, 628 insertions(+) create mode 100644 include/background.h create mode 100644 include/cli.h create mode 100644 include/cli_readline.h create mode 100644 include/cmd_mem.h create mode 100644 include/command.h create mode 100644 include/common.h create mode 100644 include/con-utils.h create mode 100644 include/config.h create mode 100644 include/crc.h create mode 100644 include/debug.h create mode 100644 include/env.h create mode 100644 include/ring.h create mode 100644 include/serial.h create mode 100644 include/timer.h create mode 100644 include/xmalloc.h create mode 100644 include/z80-if.h (limited to 'include') diff --git a/include/background.h b/include/background.h new file mode 100644 index 0000000..a624dbb --- /dev/null +++ b/include/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/include/cli.h b/include/cli.h new file mode 100644 index 0000000..67ff63b --- /dev/null +++ b/include/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/include/cli_readline.h b/include/cli_readline.h new file mode 100644 index 0000000..5b25762 --- /dev/null +++ b/include/cli_readline.h @@ -0,0 +1,50 @@ +/* + * (C) Copyright 2014 Google, Inc + * Simon Glass + * + * 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/include/cmd_mem.h b/include/cmd_mem.h new file mode 100644 index 0000000..cf379ce --- /dev/null +++ b/include/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 command_ret_t do_mem_md(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_mm(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_nm(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_mw(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_cp(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_cmp(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_base(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_loop(cmd_tbl_t *, int, int, char * const []); +#ifdef CONFIG_LOOPW +extern command_ret_t do_mem_loopw(cmd_tbl_t *, int, int, char * const []); +#endif +#ifdef CONFIG_CMD_MEMTEST +extern command_ret_t do_mem_mtest(cmd_tbl_t *, int, int, char * const []); +#endif +#ifdef CONFIG_MX_CYCLIC +extern command_ret_t do_mem_mdc(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_mem_mwc(cmd_tbl_t *, int, int, char * const []); +#endif /* CONFIG_MX_CYCLIC */ + +#endif /* CMD_MEM_H */ + diff --git a/include/command.h b/include/command.h new file mode 100644 index 0000000..2fd20a0 --- /dev/null +++ b/include/command.h @@ -0,0 +1,158 @@ + +/* + * 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 + +/* + * 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. + */ +typedef enum { + CMD_RET_SUCCESS, /* 0 = Success */ + CMD_RET_FAILURE, /* 1 = Failure */ + CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */ +} command_ret_t; + +/* + * 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 */ + command_ret_t (*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 command_ret_t do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); + +/** + * 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 + */ +command_ret_t +cmd_process(int flag, int argc, char * const argv[], uint_fast8_t *repeatable); + + +/* command.c */ +command_ret_t _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); +command_ret_t 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 FLASH 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 command_ret_t do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); +#endif +#ifdef CONFIG_CMD_BOOTM +extern command_ret_t 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 command_ret_t do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); + +/* + * 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 command_ret_t (*do_cmd_t)(cmd_tbl_t *, int, int, char * const []); + +extern cmd_tbl_t cmd_tbl[]; + + +#endif /* __COMMAND_H */ diff --git a/include/common.h b/include/common.h new file mode 100644 index 0000000..a92f62c --- /dev/null +++ b/include/common.h @@ -0,0 +1,61 @@ +#ifndef COMMON_H +#define COMMON_H + +#ifdef __AVR__ +#include + +//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 + +#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/include/con-utils.h b/include/con-utils.h new file mode 100644 index 0000000..f03dfb3 --- /dev/null +++ b/include/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/include/config.h b/include/config.h new file mode 100644 index 0000000..08d97bd --- /dev/null +++ b/include/config.h @@ -0,0 +1,29 @@ +#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 "=> " + + +/* TODO: */ +//#define CONFIG_CMDLINE_EDITING 1 +//#define CONFIG_AUTO_COMPLETE 1 + +#define CONFIG_SYS_LONGHELP 1 + +#endif /* CONFIG_H */ + diff --git a/include/crc.h b/include/crc.h new file mode 100644 index 0000000..c38bae4 --- /dev/null +++ b/include/crc.h @@ -0,0 +1,12 @@ +#ifndef CRC_H +#define CRC_H + +#include + +static inline +uint16_t crc16(uint16_t crc, uint8_t data) +{ + return _crc_ccitt_update(crc, data); +} + +#endif /* CRC_H */ diff --git a/include/debug.h b/include/debug.h new file mode 100644 index 0000000..7c19e40 --- /dev/null +++ b/include/debug.h @@ -0,0 +1,38 @@ + +#ifndef DEBUG_H_ +#define DEBUG_H_ + +#include "common.h" +#include + +#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 */ + + +void printfreelist(const char * title); + + +#endif /* DEBUG_H_ */ + diff --git a/include/env.h b/include/env.h new file mode 100644 index 0000000..9d55273 --- /dev/null +++ b/include/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/include/ring.h b/include/ring.h new file mode 100644 index 0000000..d57f9aa --- /dev/null +++ b/include/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/include/serial.h b/include/serial.h new file mode 100644 index 0000000..7414ef1 --- /dev/null +++ b/include/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/include/timer.h b/include/timer.h new file mode 100644 index 0000000..bed3eb0 --- /dev/null +++ b/include/timer.h @@ -0,0 +1,7 @@ +#ifndef TIMER_H +#define TIMER_H + +uint32_t get_timer(uint32_t); + +#endif /* TIMER_H */ + diff --git a/include/xmalloc.h b/include/xmalloc.h new file mode 100644 index 0000000..cb0019f --- /dev/null +++ b/include/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/include/z80-if.h b/include/z80-if.h new file mode 100644 index 0000000..b02fe23 --- /dev/null +++ b/include/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); -- cgit v1.2.3