From eccb333d9c206fe9db42e111b9b67f61399ce47f Mon Sep 17 00:00:00 2001 From: wangkaifan Date: Wed, 18 Nov 2020 21:50:44 +0800 Subject: [PATCH] device: support uartlite * note that serial input is not supported currently --- src/device/device.c | 8 +++- src/device/uartlite.c | 91 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/device/uartlite.c diff --git a/src/device/device.c b/src/device/device.c index 4196c7ed..1427aa98 100644 --- a/src/device/device.c +++ b/src/device/device.c @@ -9,6 +9,7 @@ void init_alarm(); void init_serial(); +void init_uartlite(); void init_timer(); void init_vga(); void init_i8042(); @@ -56,9 +57,12 @@ void sdl_clear_event_queue() { } void init_device() { - init_serial(); +#ifdef XIANGSHAN + init_vga(); + init_uartlite(); +#else init_vga(); -#ifndef XIANGSHAN + init_serial(); init_timer(); init_i8042(); init_audio(); diff --git a/src/device/uartlite.c b/src/device/uartlite.c new file mode 100644 index 00000000..0892f219 --- /dev/null +++ b/src/device/uartlite.c @@ -0,0 +1,91 @@ +#include + +#define CH_OFFSET 0 +#define UARTLITE_RX_FIFO 0x0 +#define UARTLITE_TX_FIFO 0x4 +#define UARTLITE_STAT_REG 0x8 +#define UARTLITE_CTRL_REG 0xc + +#define UARTLITE_RST_FIFO 0x03 +#define UARTLITE_TX_FULL 0x08 +#define UARTLITE_RX_VALID 0x01 + +// #define QUEUE_SIZE 1024 +// static char queue[QUEUE_SIZE] = {}; +// static int f = 0, r = 0; + +static uint8_t *serial_base = NULL; + +// static void serial_enqueue(char ch) { +// int next = (r + 1) % QUEUE_SIZE; +// if (next != f) { +// // not full +// queue[r] = ch; +// r = next; +// } +// } + +// static char serial_dequeue() { +// char ch = 0xff; + +// extern uint32_t uptime(); +// static uint32_t last = 0; +// uint32_t now = uptime(); +// if (now > last) { +// Log("now = %d", now); +// last = now; +// } +// // 90s after starting +// if (now > 90) { +// if (f != r) { +// ch = queue[f]; +// f = (f + 1) % QUEUE_SIZE; +// } +// } +// return ch; +// } + +static void serial_io_handler(uint32_t offset, int len, nemu_bool is_write) { + assert(len == 1); + switch (offset) { + /* We bind the serial port with the host stdout in NEMU. */ + case UARTLITE_TX_FIFO: + if (is_write) putc(serial_base[UARTLITE_TX_FIFO], stderr); + else panic("Cannot read UARTLITE_TX_FIFO"); + break; + case UARTLITE_STAT_REG: + if (!is_write) serial_base[UARTLITE_STAT_REG] = 0x0; + break; + } +} + +// #define rt_thread_cmd "memtrace\n" +// #define busybox_cmd "ls\n" \ +// "cd /root\n" \ +// "echo hello2\n" \ +// "cd /root/benchmark\n" \ +// "./stream\n" \ +// "echo hello3\n" \ +// "cd /root/redis\n" \ +// "ls\n" \ +// "ifconfig -a\n" \ +// "ls\n" \ +// "./redis-server\n" \ + +// #define debian_cmd "root\n" \ + +// static void preset_input() { +// char buf[] = debian_cmd; +// int i; +// for (i = 0; i < strlen(buf); i ++) { +// serial_enqueue(buf[i]); +// } +// } + +void init_uartlite() { + serial_base = new_space(0xd); + add_pio_map("uartlite", SERIAL_PORT, serial_base, 0xd, serial_io_handler); + add_mmio_map("uartlite", SERIAL_MMIO, serial_base, 0xd, serial_io_handler); + + preset_input(); +} -- GitLab