From 12284d19a97364db0ab33a6228cd1d292f3670d6 Mon Sep 17 00:00:00 2001 From: Byron Lathi Date: Mon, 18 Apr 2022 12:43:29 -0500 Subject: [PATCH] Create boot environment The boot environment will read the boot sector from the sd card, verify that it has the boot signature at the end, then jump to the start of it. From there, there should be a bootloader written to the boot segment that can handle the rest. It might be tight to fit everything into the boot sector but do remember that you do not have to initialize or select the sd card, and those functions take up a lot of space. --- sw/boot/Makefile | 42 +++++++++++++++++++++++++++++++++++ sw/boot/boot.s | 17 ++++++++++++++ sw/boot/devices/io.inc65 | 1 + sw/boot/devices/sd_card.c | 1 + sw/boot/devices/sd_card.h | 1 + sw/boot/devices/sd_card_asm.s | 1 + sw/boot/link.ld | 18 +++++++++++++++ sw/boot/load_bootsect.c | 29 ++++++++++++++++++++++++ 8 files changed, 110 insertions(+) create mode 100644 sw/boot/Makefile create mode 100644 sw/boot/boot.s create mode 120000 sw/boot/devices/io.inc65 create mode 120000 sw/boot/devices/sd_card.c create mode 120000 sw/boot/devices/sd_card.h create mode 120000 sw/boot/devices/sd_card_asm.s create mode 100644 sw/boot/link.ld create mode 100644 sw/boot/load_bootsect.c diff --git a/sw/boot/Makefile b/sw/boot/Makefile new file mode 100644 index 0000000..a8b4fde --- /dev/null +++ b/sw/boot/Makefile @@ -0,0 +1,42 @@ +CC=cl65 +CFLAGS=-T -t none -I. --cpu "65C02" +test: CFLAGS=-T -t sim65c02 -I. +LDFLAGS=-C link.ld -m $(NAME).map +SIM=sim65 +SIMARGS=-v -c -x 1000000 + +NAME=bootrom + +TEST_BIN=test.bin +BIN=$(NAME).bin +HEX=$(NAME).hex + +LISTS=lists + +SRCS=$(wildcard *.s) $(wildcard *.c) +SRCS+=$(filter-out $(wildcard tests/*), $(wildcard **/*.s)) $(filter-out $(wildcard tests/*), $(wildcard **/*.c)) +OBJS+=$(patsubst %.s,%.o,$(filter %s,$(SRCS))) +OBJS+=$(patsubst %.c,%.o,$(filter %c,$(SRCS))) + +all: $(HEX) + +$(HEX): $(BIN) + objcopy --input-target=binary --output-target=ihex $(BIN) $(HEX) + + +$(BIN): $(OBJS) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@ + +%.o: %.c $(LISTS) + $(CC) $(CFLAGS) -l $(LISTS)/$<.list -c $< -o $@ + +%.o: %.s $(LISTS) + $(CC) $(CFLAGS) -l $(LISTS)/$<.list -c $< -o $@ + +$(LISTS): + mkdir -p $(addprefix $(LISTS)/,$(sort $(dir $(SRCS)))) + +.PHONY: clean +clean: + rm -rf $(OBJS) $(BIN) $(HEX) $(LISTS) $(NAME).map + diff --git a/sw/boot/boot.s b/sw/boot/boot.s new file mode 100644 index 0000000..1cd5448 --- /dev/null +++ b/sw/boot/boot.s @@ -0,0 +1,17 @@ +; We need to to read the boot sector from the +; SD card, verify the last 2 bytes, then jump to the +; beginning of it. + +.include "zeropage.inc" + +.segment "STARTUP" + +.import _load_bootsect + +_init: ldx #$ff + txs + cld + + jsr _load_bootsect + jmp $1000 + \ No newline at end of file diff --git a/sw/boot/devices/io.inc65 b/sw/boot/devices/io.inc65 new file mode 120000 index 0000000..14ef1f5 --- /dev/null +++ b/sw/boot/devices/io.inc65 @@ -0,0 +1 @@ +../../kernel/devices/io.inc65 \ No newline at end of file diff --git a/sw/boot/devices/sd_card.c b/sw/boot/devices/sd_card.c new file mode 120000 index 0000000..c20d8c7 --- /dev/null +++ b/sw/boot/devices/sd_card.c @@ -0,0 +1 @@ +../../kernel/devices/sd_card.c \ No newline at end of file diff --git a/sw/boot/devices/sd_card.h b/sw/boot/devices/sd_card.h new file mode 120000 index 0000000..7955199 --- /dev/null +++ b/sw/boot/devices/sd_card.h @@ -0,0 +1 @@ +../../kernel/devices/sd_card.h \ No newline at end of file diff --git a/sw/boot/devices/sd_card_asm.s b/sw/boot/devices/sd_card_asm.s new file mode 120000 index 0000000..a25313c --- /dev/null +++ b/sw/boot/devices/sd_card_asm.s @@ -0,0 +1 @@ +../../kernel/devices/sd_card_asm.s \ No newline at end of file diff --git a/sw/boot/link.ld b/sw/boot/link.ld new file mode 100644 index 0000000..33de787 --- /dev/null +++ b/sw/boot/link.ld @@ -0,0 +1,18 @@ +MEMORY +{ + ZP: start = $0, size = $100, type = rw, define = yes; + SDRAM: start = $1000, size = $6ef0, type = rw, define = yes; + ROM: start = $8000, size = $8000, fill = yes, fillval = $ff, file = %O; +} + +SEGMENTS { + ZEROPAGE: load = ZP, type = zp, define = yes; + DATA: load = ROM, type = rw, define = yes, run = SDRAM; + BSS: load = SDRAM, type = bss, define = yes; + HEAP: load = SDRAM, type = bss, optional = yes; + STARTUP: load = ROM, type = ro; + ONCE: load = ROM, type = ro, optional = yes; + CODE: load = ROM, type = ro; + RODATA: load = ROM, type = ro; +} + diff --git a/sw/boot/load_bootsect.c b/sw/boot/load_bootsect.c new file mode 100644 index 0000000..6fe0ba2 --- /dev/null +++ b/sw/boot/load_bootsect.c @@ -0,0 +1,29 @@ +#include + +#include "devices/sd_card.h" + +#define BOOTSECTOR_LOAD_ADDRESS 0x1000 + +#define BOOTSIG_0 0x55 +#define BOOTSIG_1 0xaa + +//Should probably do this in asm +void load_bootsect() { + uint32_t rca; + uint8_t sig[2]; + + sd_init(); + rca = sd_get_rca(); + sd_select_card(rca); + + sd_readblock(0, (uint8_t*)BOOTSECTOR_LOAD_ADDRESS); + + sig[0] = ((uint8_t*)BOOTSECTOR_LOAD_ADDRESS)[510]; + sig[1] = ((uint8_t*)BOOTSECTOR_LOAD_ADDRESS)[511]; + + if (sig[0] != BOOTSIG_0 || sig[1] != BOOTSIG_1) { + for(;;); //maybe figure out a way to have an error message here + } + + return; +} \ No newline at end of file