From 3e8cc817b7fe7de4b123185f2adcdf49623515bd Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 20 Nov 2019 11:14:01 -0700 Subject: [PATCH] Seperate compilation of scratch rom --- src/board/system76/galp3-c/board.mk | 34 +++++++++ src/board/system76/galp3-c/scratch.c | 84 ++++++++++------------- src/board/system76/galp3-c/scratch/main.c | 34 +++++++++ 3 files changed, 103 insertions(+), 49 deletions(-) create mode 100644 src/board/system76/galp3-c/scratch/main.c diff --git a/src/board/system76/galp3-c/board.mk b/src/board/system76/galp3-c/board.mk index e1a3a97..eef4650 100644 --- a/src/board/system76/galp3-c/board.mk +++ b/src/board/system76/galp3-c/board.mk @@ -8,3 +8,37 @@ CFLAGS+=-DLEVEL=2 # Enable I2C debug on 0x76 # CFLAGS+=-DI2C_DEBUGGER=0x76 + +# Add scratch ROM source +SCRATCH_SRC=$(wildcard $(BOARD_DIR)/scratch/*.c) +SCRATCH_OBJ=$(patsubst src/%.c,$(BUILD)/%.rel,$(SCRATCH_SRC)) +SCRATCH_CC=\ + sdcc \ + -mmcs51 \ + --model-small \ + --code-size 1024 \ + --Werror + +# Convert from binary file to C header +$(BUILD)/include/scratch.h: $(BUILD)/scratch.rom + @mkdir -p $(@D) + xxd --include < $< > $@ + +# Convert from Intel Hex file to binary file +$(BUILD)/scratch.rom: $(BUILD)/scratch.ihx + @mkdir -p $(@D) + makebin -p < $< > $@ + +# Link object files into Intel Hex file +$(BUILD)/scratch.ihx: $(SCRATCH_OBJ) + @mkdir -p $(@D) + $(SCRATCH_CC) -o $@ $^ + +# Compile C files into object files +$(SCRATCH_OBJ): $(BUILD)/%.rel: src/%.c + @mkdir -p $(@D) + $(SCRATCH_CC) -o $@ -c $< + +# Require scratch.rom to be compiled before main firmware +CFLAGS+=-I$(BUILD)/include +INCLUDE+=$(BUILD)/include/scratch.h diff --git a/src/board/system76/galp3-c/scratch.c b/src/board/system76/galp3-c/scratch.c index 894cc23..6b089d2 100644 --- a/src/board/system76/galp3-c/scratch.c +++ b/src/board/system76/galp3-c/scratch.c @@ -1,21 +1,22 @@ #include #include -volatile uint8_t __xdata __at(0x103B) ECINDAR0; -volatile uint8_t __xdata __at(0x103C) ECINDAR1; -volatile uint8_t __xdata __at(0x103D) ECINDAR2; -volatile uint8_t __xdata __at(0x103E) ECINDAR3; -volatile uint8_t __xdata __at(0x103F) ECINDDR; -volatile uint8_t __xdata __at(0x1040) SCAR0L; -volatile uint8_t __xdata __at(0x1041) SCAR0M; -volatile uint8_t __xdata __at(0x1042) SCAR0H; +#include + +// Include scratch ROM, must be less than 1024 bytes, controlled by makefile +uint8_t __code scratch_rom[] = { + #include +}; + +// Scratch RAM 1 is 1024 bytes, located at 2048 bytes +volatile uint8_t __xdata __at(0x0800) scratch_ram_1[1024]; + volatile uint8_t __xdata __at(0x1043) SCAR1L; volatile uint8_t __xdata __at(0x1044) SCAR1M; volatile uint8_t __xdata __at(0x1045) SCAR1H; -// Create new segment located at 0x1000 -static void scratch_start(void) __naked -{ +// Create new segment located at 0x1000, after scratch ROM mapping +static void scratch_start(void) __naked { __asm .area SCRATCH (ABS,CODE) __endasm; @@ -24,52 +25,37 @@ static void scratch_start(void) __naked __endasm; } -#define FLASH_SPI 0x00000000 -#define FLASH_EMBEDDED 0x40000000 +// Enter scratch ROM +int scratch_trampoline(void) { + // Uses SCAR1 which is mapped at 0x0800 in data space and is 1024 bytes in + // size. SCAR0 cannot be used due to __pdata overwriting it. -static int flash_transaction(uint32_t offset, uint8_t * data, int length, bool read) { int i; - for (i = 0; i < length; i++, offset++) { - ECINDAR3 = (uint8_t)(offset >> 24); - ECINDAR2 = (uint8_t)(offset >> 16); - ECINDAR1 = (uint8_t)(offset >> 8); - ECINDAR0 = (uint8_t)(offset); - if (read) { - data[i] = ECINDDR; - } else { - ECINDDR = data[i]; - } + // Copy scratch ROM + for (i = 0; i < ARRAY_SIZE(scratch_rom) && i < ARRAY_SIZE(scratch_ram_1); i++) { + scratch_ram_1[i] = scratch_rom[i]; } - return i; -} -// Main program while running in scratch ROM -static void scratch_main(void) { - // TODO: implement flashing protocol and prevent access to flash - // data (i.e. calling functions in CSEG) during this process - for (;;) {} + // Fill the rest with nop + for (; i < ARRAY_SIZE(scratch_ram_1); i++) { + scratch_ram_1[i] = 0x00; + } + + // Set mapping at 0x0000 and enable + SCAR1L = 0x00; + SCAR1M = 0x00; + SCAR1H = 0x00; + + // Jump to scratch reset function + __asm__("ljmp 0"); + + // Should never happen + return 0; } // Finish segment located at 0x1000 -static void scratch_end(void) __naked -{ +static void scratch_end(void) __naked { __asm .area CSEG (REL,CODE) __endasm; } - -// Enter scratch ROM -void scratch_trampoline(void) { - // Uses SCAR1 which is mapped at 0x0800 in data space and is 1024 bytes in - // size. SCAR0 cannot be used due to __pdata overwriting it. - - // Set flag to use DMA on next write - SCAR1H = 0x80; - // Set mapping at 0x1000 - SCAR1L = 0x00; - SCAR1M = 0x10; - // Set DMA and enable code space mapping - SCAR1H = 0x00; - - scratch_main(); -} diff --git a/src/board/system76/galp3-c/scratch/main.c b/src/board/system76/galp3-c/scratch/main.c new file mode 100644 index 0000000..ff286b0 --- /dev/null +++ b/src/board/system76/galp3-c/scratch/main.c @@ -0,0 +1,34 @@ +#include +#include + +volatile uint8_t __xdata __at(0x103B) ECINDAR0; +volatile uint8_t __xdata __at(0x103C) ECINDAR1; +volatile uint8_t __xdata __at(0x103D) ECINDAR2; +volatile uint8_t __xdata __at(0x103E) ECINDAR3; +volatile uint8_t __xdata __at(0x103F) ECINDDR; + +#define FLASH_SPI 0x00000000 +#define FLASH_EMBEDDED 0x40000000 + +static int flash_transaction(uint32_t offset, uint8_t * data, int length, bool read) { + int i; + for (i = 0; i < length; i++, offset++) { + ECINDAR3 = (uint8_t)(offset >> 24); + ECINDAR2 = (uint8_t)(offset >> 16); + ECINDAR1 = (uint8_t)(offset >> 8); + ECINDAR0 = (uint8_t)(offset); + if (read) { + data[i] = ECINDDR; + } else { + ECINDDR = data[i]; + } + } + return i; +} + +// Main program while running in scratch ROM +void main(void) { + // TODO: implement flashing protocol and prevent access to flash + // data (i.e. calling functions in CSEG) during this process + for (;;) {} +}