NJU-ProjectN/abstract-machine ics2023 initialized
NJU-ProjectN/abstract-machine 3348db971fd860be5cb28e21c18f9d0e65d0c96a Merge pull request #8 from Jasonyanyusong/master
This commit is contained in:
parent
2824efad33
commit
8e4feb4010
129 changed files with 9017 additions and 0 deletions
19
abstract-machine/am/src/platform/dummy/cte.c
Normal file
19
abstract-machine/am/src/platform/dummy/cte.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include <am.h>
|
||||
|
||||
bool cte_init(Context*(*handler)(Event, Context*)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Context *kcontext(Area kstack, void (*entry)(void *), void *arg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void yield() {
|
||||
}
|
||||
|
||||
bool ienabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
void iset(bool enable) {
|
||||
}
|
11
abstract-machine/am/src/platform/dummy/ioe.c
Normal file
11
abstract-machine/am/src/platform/dummy/ioe.c
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <am.h>
|
||||
#include <klib-macros.h>
|
||||
|
||||
static void fail(void *buf) { panic("access nonexist register"); }
|
||||
|
||||
bool ioe_init() {
|
||||
return false;
|
||||
}
|
||||
|
||||
void ioe_read (int reg, void *buf) { fail(buf); }
|
||||
void ioe_write(int reg, void *buf) { fail(buf); }
|
17
abstract-machine/am/src/platform/dummy/mpe.c
Normal file
17
abstract-machine/am/src/platform/dummy/mpe.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include <am.h>
|
||||
|
||||
bool mpe_init(void (*entry)()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int cpu_count() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cpu_current() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int atomic_xchg(int *addr, int newval) {
|
||||
return 0;
|
||||
}
|
10
abstract-machine/am/src/platform/dummy/trm.c
Normal file
10
abstract-machine/am/src/platform/dummy/trm.c
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include <am.h>
|
||||
|
||||
Area heap = RANGE(NULL, NULL);
|
||||
|
||||
void putch(char ch) {
|
||||
}
|
||||
|
||||
void halt(int code) {
|
||||
while (1);
|
||||
}
|
18
abstract-machine/am/src/platform/dummy/vme.c
Normal file
18
abstract-machine/am/src/platform/dummy/vme.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include <am.h>
|
||||
|
||||
bool vme_init(void* (*pgalloc_f)(int), void (*pgfree_f)(void*)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void protect(AddrSpace *as) {
|
||||
}
|
||||
|
||||
void unprotect(AddrSpace *as) {
|
||||
}
|
||||
|
||||
void map(AddrSpace *as, void *va, void *pa, int prot) {
|
||||
}
|
||||
|
||||
Context *ucontext(AddrSpace *as, Area kstack, void *entry) {
|
||||
return NULL;
|
||||
}
|
50
abstract-machine/am/src/platform/nemu/include/nemu.h
Normal file
50
abstract-machine/am/src/platform/nemu/include/nemu.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
#ifndef NEMU_H__
|
||||
#define NEMU_H__
|
||||
|
||||
#include <klib-macros.h>
|
||||
|
||||
#include ISA_H // the macro `ISA_H` is defined in CFLAGS
|
||||
// it will be expanded as "x86/x86.h", "mips/mips32.h", ...
|
||||
|
||||
#if defined(__ISA_X86__)
|
||||
# define nemu_trap(code) asm volatile ("int3" : :"a"(code))
|
||||
#elif defined(__ISA_MIPS32__)
|
||||
# define nemu_trap(code) asm volatile ("move $v0, %0; sdbbp" : :"r"(code))
|
||||
#elif defined(__riscv)
|
||||
# define nemu_trap(code) asm volatile("mv a0, %0; ebreak" : :"r"(code))
|
||||
#elif defined(__ISA_LOONGARCH32R__)
|
||||
# define nemu_trap(code) asm volatile("move $a0, %0; break 0" : :"r"(code))
|
||||
#elif
|
||||
# error unsupported ISA __ISA__
|
||||
#endif
|
||||
|
||||
#if defined(__ARCH_X86_NEMU)
|
||||
# define DEVICE_BASE 0x0
|
||||
#else
|
||||
# define DEVICE_BASE 0xa0000000
|
||||
#endif
|
||||
|
||||
#define MMIO_BASE 0xa0000000
|
||||
|
||||
#define SERIAL_PORT (DEVICE_BASE + 0x00003f8)
|
||||
#define KBD_ADDR (DEVICE_BASE + 0x0000060)
|
||||
#define RTC_ADDR (DEVICE_BASE + 0x0000048)
|
||||
#define VGACTL_ADDR (DEVICE_BASE + 0x0000100)
|
||||
#define AUDIO_ADDR (DEVICE_BASE + 0x0000200)
|
||||
#define DISK_ADDR (DEVICE_BASE + 0x0000300)
|
||||
#define FB_ADDR (MMIO_BASE + 0x1000000)
|
||||
#define AUDIO_SBUF_ADDR (MMIO_BASE + 0x1200000)
|
||||
|
||||
extern char _pmem_start;
|
||||
#define PMEM_SIZE (128 * 1024 * 1024)
|
||||
#define PMEM_END ((uintptr_t)&_pmem_start + PMEM_SIZE)
|
||||
#define NEMU_PADDR_SPACE \
|
||||
RANGE(&_pmem_start, PMEM_END), \
|
||||
RANGE(FB_ADDR, FB_ADDR + 0x200000), \
|
||||
RANGE(MMIO_BASE, MMIO_BASE + 0x1000) /* serial, rtc, screen, keyboard */
|
||||
|
||||
typedef uintptr_t PTE;
|
||||
|
||||
#define PGSIZE 4096
|
||||
|
||||
#endif
|
26
abstract-machine/am/src/platform/nemu/ioe/audio.c
Normal file
26
abstract-machine/am/src/platform/nemu/ioe/audio.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <am.h>
|
||||
#include <nemu.h>
|
||||
|
||||
#define AUDIO_FREQ_ADDR (AUDIO_ADDR + 0x00)
|
||||
#define AUDIO_CHANNELS_ADDR (AUDIO_ADDR + 0x04)
|
||||
#define AUDIO_SAMPLES_ADDR (AUDIO_ADDR + 0x08)
|
||||
#define AUDIO_SBUF_SIZE_ADDR (AUDIO_ADDR + 0x0c)
|
||||
#define AUDIO_INIT_ADDR (AUDIO_ADDR + 0x10)
|
||||
#define AUDIO_COUNT_ADDR (AUDIO_ADDR + 0x14)
|
||||
|
||||
void __am_audio_init() {
|
||||
}
|
||||
|
||||
void __am_audio_config(AM_AUDIO_CONFIG_T *cfg) {
|
||||
cfg->present = false;
|
||||
}
|
||||
|
||||
void __am_audio_ctrl(AM_AUDIO_CTRL_T *ctrl) {
|
||||
}
|
||||
|
||||
void __am_audio_status(AM_AUDIO_STATUS_T *stat) {
|
||||
stat->count = 0;
|
||||
}
|
||||
|
||||
void __am_audio_play(AM_AUDIO_PLAY_T *ctl) {
|
||||
}
|
12
abstract-machine/am/src/platform/nemu/ioe/disk.c
Normal file
12
abstract-machine/am/src/platform/nemu/ioe/disk.c
Normal file
|
@ -0,0 +1,12 @@
|
|||
#include <am.h>
|
||||
#include <nemu.h>
|
||||
|
||||
void __am_disk_config(AM_DISK_CONFIG_T *cfg) {
|
||||
cfg->present = false;
|
||||
}
|
||||
|
||||
void __am_disk_status(AM_DISK_STATUS_T *stat) {
|
||||
}
|
||||
|
||||
void __am_disk_blkio(AM_DISK_BLKIO_T *io) {
|
||||
}
|
25
abstract-machine/am/src/platform/nemu/ioe/gpu.c
Normal file
25
abstract-machine/am/src/platform/nemu/ioe/gpu.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include <am.h>
|
||||
#include <nemu.h>
|
||||
|
||||
#define SYNC_ADDR (VGACTL_ADDR + 4)
|
||||
|
||||
void __am_gpu_init() {
|
||||
}
|
||||
|
||||
void __am_gpu_config(AM_GPU_CONFIG_T *cfg) {
|
||||
*cfg = (AM_GPU_CONFIG_T) {
|
||||
.present = true, .has_accel = false,
|
||||
.width = 0, .height = 0,
|
||||
.vmemsz = 0
|
||||
};
|
||||
}
|
||||
|
||||
void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *ctl) {
|
||||
if (ctl->sync) {
|
||||
outl(SYNC_ADDR, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void __am_gpu_status(AM_GPU_STATUS_T *status) {
|
||||
status->ready = true;
|
||||
}
|
9
abstract-machine/am/src/platform/nemu/ioe/input.c
Normal file
9
abstract-machine/am/src/platform/nemu/ioe/input.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include <am.h>
|
||||
#include <nemu.h>
|
||||
|
||||
#define KEYDOWN_MASK 0x8000
|
||||
|
||||
void __am_input_keybrd(AM_INPUT_KEYBRD_T *kbd) {
|
||||
kbd->keydown = 0;
|
||||
kbd->keycode = AM_KEY_NONE;
|
||||
}
|
59
abstract-machine/am/src/platform/nemu/ioe/ioe.c
Normal file
59
abstract-machine/am/src/platform/nemu/ioe/ioe.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include <am.h>
|
||||
#include <klib-macros.h>
|
||||
|
||||
void __am_timer_init();
|
||||
void __am_gpu_init();
|
||||
void __am_audio_init();
|
||||
void __am_input_keybrd(AM_INPUT_KEYBRD_T *);
|
||||
void __am_timer_rtc(AM_TIMER_RTC_T *);
|
||||
void __am_timer_uptime(AM_TIMER_UPTIME_T *);
|
||||
void __am_gpu_config(AM_GPU_CONFIG_T *);
|
||||
void __am_gpu_status(AM_GPU_STATUS_T *);
|
||||
void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *);
|
||||
void __am_audio_config(AM_AUDIO_CONFIG_T *);
|
||||
void __am_audio_ctrl(AM_AUDIO_CTRL_T *);
|
||||
void __am_audio_status(AM_AUDIO_STATUS_T *);
|
||||
void __am_audio_play(AM_AUDIO_PLAY_T *);
|
||||
void __am_disk_config(AM_DISK_CONFIG_T *cfg);
|
||||
void __am_disk_status(AM_DISK_STATUS_T *stat);
|
||||
void __am_disk_blkio(AM_DISK_BLKIO_T *io);
|
||||
|
||||
static void __am_timer_config(AM_TIMER_CONFIG_T *cfg) { cfg->present = true; cfg->has_rtc = true; }
|
||||
static void __am_input_config(AM_INPUT_CONFIG_T *cfg) { cfg->present = true; }
|
||||
static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; }
|
||||
static void __am_net_config (AM_NET_CONFIG_T *cfg) { cfg->present = false; }
|
||||
|
||||
typedef void (*handler_t)(void *buf);
|
||||
static void *lut[128] = {
|
||||
[AM_TIMER_CONFIG] = __am_timer_config,
|
||||
[AM_TIMER_RTC ] = __am_timer_rtc,
|
||||
[AM_TIMER_UPTIME] = __am_timer_uptime,
|
||||
[AM_INPUT_CONFIG] = __am_input_config,
|
||||
[AM_INPUT_KEYBRD] = __am_input_keybrd,
|
||||
[AM_GPU_CONFIG ] = __am_gpu_config,
|
||||
[AM_GPU_FBDRAW ] = __am_gpu_fbdraw,
|
||||
[AM_GPU_STATUS ] = __am_gpu_status,
|
||||
[AM_UART_CONFIG ] = __am_uart_config,
|
||||
[AM_AUDIO_CONFIG] = __am_audio_config,
|
||||
[AM_AUDIO_CTRL ] = __am_audio_ctrl,
|
||||
[AM_AUDIO_STATUS] = __am_audio_status,
|
||||
[AM_AUDIO_PLAY ] = __am_audio_play,
|
||||
[AM_DISK_CONFIG ] = __am_disk_config,
|
||||
[AM_DISK_STATUS ] = __am_disk_status,
|
||||
[AM_DISK_BLKIO ] = __am_disk_blkio,
|
||||
[AM_NET_CONFIG ] = __am_net_config,
|
||||
};
|
||||
|
||||
static void fail(void *buf) { panic("access nonexist register"); }
|
||||
|
||||
bool ioe_init() {
|
||||
for (int i = 0; i < LENGTH(lut); i++)
|
||||
if (!lut[i]) lut[i] = fail;
|
||||
__am_gpu_init();
|
||||
__am_timer_init();
|
||||
__am_audio_init();
|
||||
return true;
|
||||
}
|
||||
|
||||
void ioe_read (int reg, void *buf) { ((handler_t)lut[reg])(buf); }
|
||||
void ioe_write(int reg, void *buf) { ((handler_t)lut[reg])(buf); }
|
18
abstract-machine/am/src/platform/nemu/ioe/timer.c
Normal file
18
abstract-machine/am/src/platform/nemu/ioe/timer.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include <am.h>
|
||||
#include <nemu.h>
|
||||
|
||||
void __am_timer_init() {
|
||||
}
|
||||
|
||||
void __am_timer_uptime(AM_TIMER_UPTIME_T *uptime) {
|
||||
uptime->us = 0;
|
||||
}
|
||||
|
||||
void __am_timer_rtc(AM_TIMER_RTC_T *rtc) {
|
||||
rtc->second = 0;
|
||||
rtc->minute = 0;
|
||||
rtc->hour = 0;
|
||||
rtc->day = 0;
|
||||
rtc->month = 0;
|
||||
rtc->year = 1900;
|
||||
}
|
20
abstract-machine/am/src/platform/nemu/mpe.c
Normal file
20
abstract-machine/am/src/platform/nemu/mpe.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include <am.h>
|
||||
#include <stdatomic.h>
|
||||
#include <klib-macros.h>
|
||||
|
||||
bool mpe_init(void (*entry)()) {
|
||||
entry();
|
||||
panic("MPE entry returns");
|
||||
}
|
||||
|
||||
int cpu_count() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cpu_current() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int atomic_xchg(int *addr, int newval) {
|
||||
return atomic_exchange(addr, newval);
|
||||
}
|
27
abstract-machine/am/src/platform/nemu/trm.c
Normal file
27
abstract-machine/am/src/platform/nemu/trm.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <am.h>
|
||||
#include <nemu.h>
|
||||
|
||||
extern char _heap_start;
|
||||
int main(const char *args);
|
||||
|
||||
Area heap = RANGE(&_heap_start, PMEM_END);
|
||||
#ifndef MAINARGS
|
||||
#define MAINARGS ""
|
||||
#endif
|
||||
static const char mainargs[] = MAINARGS;
|
||||
|
||||
void putch(char ch) {
|
||||
outb(SERIAL_PORT, ch);
|
||||
}
|
||||
|
||||
void halt(int code) {
|
||||
nemu_trap(code);
|
||||
|
||||
// should not reach here
|
||||
while (1);
|
||||
}
|
||||
|
||||
void _trm_init() {
|
||||
int ret = main(mainargs);
|
||||
halt(ret);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue