From 9dd460a47fc6246c8f375878a44fc50cbd7241d8 Mon Sep 17 00:00:00 2001 From: Byron Lathi Date: Mon, 18 Apr 2022 20:25:08 -0500 Subject: [PATCH] Add preliminary bootloader This bootloader right now just prints the letter A, but should be capable of reading blocks from the SD card into memory. --- sw/bootloader/Makefile | 41 ++++++++++++++++++++++++++++++++++++++ sw/bootloader/bios_calls | 0 sw/bootloader/bios_calls.h | 8 ++++++++ sw/bootloader/bios_calls.s | 23 +++++++++++++++++++++ sw/bootloader/boot.s | 26 ++++++++++++++++++++++++ sw/bootloader/io.inc65 | 1 + sw/bootloader/link.ld | 17 ++++++++++++++++ sw/bootloader/main.c | 11 ++++++++++ sw/bootloader/uart.h | 1 + sw/bootloader/uart.s | 1 + 10 files changed, 129 insertions(+) create mode 100644 sw/bootloader/Makefile create mode 100644 sw/bootloader/bios_calls create mode 100644 sw/bootloader/bios_calls.h create mode 100644 sw/bootloader/bios_calls.s create mode 100644 sw/bootloader/boot.s create mode 120000 sw/bootloader/io.inc65 create mode 100644 sw/bootloader/link.ld create mode 100644 sw/bootloader/main.c create mode 120000 sw/bootloader/uart.h create mode 120000 sw/bootloader/uart.s diff --git a/sw/bootloader/Makefile b/sw/bootloader/Makefile new file mode 100644 index 0000000..2365468 --- /dev/null +++ b/sw/bootloader/Makefile @@ -0,0 +1,41 @@ +CC=cl65 +CFLAGS=-T -t none -I. --cpu "65C02" -DNO_SD_INIT +test: CFLAGS=-T -t sim65c02 -I. +LDFLAGS=-C link.ld -m $(NAME).map +SIM=sim65 +SIMARGS=-v -c -x 1000000 + +NAME=bootloader + +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/bootloader/bios_calls b/sw/bootloader/bios_calls new file mode 100644 index 0000000..e69de29 diff --git a/sw/bootloader/bios_calls.h b/sw/bootloader/bios_calls.h new file mode 100644 index 0000000..88ed750 --- /dev/null +++ b/sw/bootloader/bios_calls.h @@ -0,0 +1,8 @@ +#ifndef _BIOS_CALLS_H +#define _BIOS_CALLS_H + +#include + +void sd_readblock(uint16_t addr, void* buf); + +#endif \ No newline at end of file diff --git a/sw/bootloader/bios_calls.s b/sw/bootloader/bios_calls.s new file mode 100644 index 0000000..a1964a3 --- /dev/null +++ b/sw/bootloader/bios_calls.s @@ -0,0 +1,23 @@ +.export _sd_readblock + +.autoimport + +.importzp sreg + +; sreg is the pointer to store the data +; a/y is the block address +; send command 17 with the block address of 00/y/a + +; void sd_readblock(uint16_t addr, void* buf); + +_sd_readblock: + sta sreg ; move buf pointer to sreg + stx sreg+1 + + jsr popax ; move addr to a/y + tya + + ldx #$1 ; call interrupt 1 + brk + + rts \ No newline at end of file diff --git a/sw/bootloader/boot.s b/sw/bootloader/boot.s new file mode 100644 index 0000000..47eaf07 --- /dev/null +++ b/sw/bootloader/boot.s @@ -0,0 +1,26 @@ +; 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" + +.import _load_bootsect + +.export _init, _boot + +.segment "STARTUP" + +_init: jmp _boot + +.segment "CODE" + +_boot: ldx #$ff + txs + cld + + jsr _load_bootsect + jmp $1000 + +.segment "SIGN" +.byte $55 +.byte $aa diff --git a/sw/bootloader/io.inc65 b/sw/bootloader/io.inc65 new file mode 120000 index 0000000..52851e4 --- /dev/null +++ b/sw/bootloader/io.inc65 @@ -0,0 +1 @@ +../kernel/devices/io.inc65 \ No newline at end of file diff --git a/sw/bootloader/link.ld b/sw/bootloader/link.ld new file mode 100644 index 0000000..6860602 --- /dev/null +++ b/sw/bootloader/link.ld @@ -0,0 +1,17 @@ +MEMORY +{ + ZP: start = $0, size = $100, type = rw, define = yes; + BOOTSECT: start = $1000, size = $200, file = %O; +} + +SEGMENTS { + ZEROPAGE: load = ZP, type = zp, define = yes; + DATA: load = BOOTSECT, type = rw, define = yes; + BSS: load = BOOTSECT, type = bss, define = yes; + HEAP: load = BOOTSECT, type = bss, optional = yes; + STARTUP: load = BOOTSECT, type = ro, start = $1000; + CODE: load = BOOTSECT, type = ro, start = $105a; + RODATA: load = BOOTSECT, type = ro; + SIGN: load = BOOTSECT, type = ro, start = $11fe; +} + diff --git a/sw/bootloader/main.c b/sw/bootloader/main.c new file mode 100644 index 0000000..e69fb86 --- /dev/null +++ b/sw/bootloader/main.c @@ -0,0 +1,11 @@ +#include + +#include "bios_calls.h" +#include "uart.h" + +//Should probably do this in asm +void load_bootsect() { + uart_txb_block('A'); + for (;;); + return; +} \ No newline at end of file diff --git a/sw/bootloader/uart.h b/sw/bootloader/uart.h new file mode 120000 index 0000000..bdfeb82 --- /dev/null +++ b/sw/bootloader/uart.h @@ -0,0 +1 @@ +../kernel/devices/uart.h \ No newline at end of file diff --git a/sw/bootloader/uart.s b/sw/bootloader/uart.s new file mode 120000 index 0000000..aa91b87 --- /dev/null +++ b/sw/bootloader/uart.s @@ -0,0 +1 @@ +../kernel/devices/uart.s \ No newline at end of file