diff --git a/Makefile b/Makefile index 74b6bf2..97f7d7d 100644 --- a/Makefile +++ b/Makefile @@ -71,13 +71,7 @@ TI84pCSE: BOOT := 3FC000 TI84pCSE: LENGTH := 0x400000 TI84pCSE: kernel $(OUTDIR) -SKIPON:=NO - -ifeq ($(SKIPON),YES) -DEFINES=--define $(PLATFORM) --define SKIPON -else DEFINES=--define $(PLATFORM) -endif BINDIR=$(OUTDIR)$(PLATFORM)/ INCLUDE=include/;$(BINDIR) diff --git a/include/kernelmem.inc b/include/kernelmem.inc index c1f2396..5f8685e 100644 --- a/include/kernelmem.inc +++ b/include/kernelmem.inc @@ -51,14 +51,10 @@ clip_mask .equ currentContrast + 2 ; 1 byte color_mode .equ clip_mask + 1 ; 1 byte +random_seed .equ color_mode + 1 ; 8 bytes + flashFunctions .equ stateMemory + stateMemorySize kernelGarbage .equ flashFunctions + flashFunctionSize -#ifdef DEBUG -debugBuffer .equ kernelGarbage + kernelGarbageSize -userMemory .equ debugBuffer + 0x300 -userMemorySize .equ 0xFFFF - userMemory ; user memory extends to 0xFFFF -#else userMemory .equ kernelGarbage + kernelGarbageSize userMemorySize .equ 0xFFFF - userMemory ; user memory extends to 0xFFFF -#endif diff --git a/src/00/base.asm b/src/00/base.asm index 010fc89..28c23a6 100644 --- a/src/00/base.asm +++ b/src/00/base.asm @@ -1,5 +1,4 @@ ; Base file for KnightOS kernel -#define DEBUG #include "platforms.inc" #include "defines.inc" @@ -44,13 +43,10 @@ drawHexHL .equ 0x0D01 #include "keyboard.asm" #include "math.asm" +#include "random.asm" #include "strings.asm" #include "util.asm" -#ifdef DEBUG -#include "debug.asm" -#endif - .echo "Assigned kernel memory:" .echo "threadTable: 0x{0:X4}" threadTable .echo "libraryTable: 0x{0:X4}" libraryTable diff --git a/src/00/boot.asm b/src/00/boot.asm index fc431dc..0f708fa 100644 --- a/src/00/boot.asm +++ b/src/00/boot.asm @@ -37,9 +37,7 @@ _: di ld sp, kernelGarbage + kernelGarbageSize -#ifndef SKIPON call suspendDevice -#endif ;; reboot [System] ;; Restarts the device. reboot: @@ -63,118 +61,24 @@ reboot: out (PORT_BANKB), a #endif - ; Manipulate protection states -#ifdef CPU15 ; TI-83+ SE, TI-84+, TI-84+ SE, TI-84+ CSE call unlockFlash - ; Remove RAM Execution Protection - xor a - out (PORT_RAMEXEC_LOWLIMIT), a ; RAM Lower Limit ; out (25), 0 - dec a - out (PORT_RAMEXEC_UPLIMIT), a ; RAM Upper Limit ; out (26), $FF - - ; Remove Flash Execution Protection - out (PORT_FLASHEXEC_LOWLIMIT), a ; Flash Lower Limit ; out (22), $FF - out (PORT_FLASHEXEC_UPLIMIT), a ; Flash Upper Limit ; out (23), $FF + call unprotectRAM + call unprotectFlash call lockFlash - ; Set CPU speed to 15 MHz +#ifdef CPU15 ld a, BIT_CPUSPEED_15MHZ out (PORT_CPUSPEED), a - -#else ; TI-73, TI-83+ - #ifndef TI73 ; RAM does not have protection on the TI-73 - - ; Remove RAM/Flash protection - call unlockFlash - xor a - out (PORT_RAM_PAGING), a - out (PORT_FLASHEXCLUSION), a - - ld a, 0b000000001 - out (PORT_RAM_PAGING), a - xor a - out (PORT_FLASHEXCLUSION), a - - ld a, 0b000000010 - out (PORT_RAM_PAGING), a - xor a - out (PORT_FLASHEXCLUSION), a - - ld a, 0b000000111 - out (PORT_RAM_PAGING), a - xor a - out (PORT_FLASHEXCLUSION), a - call lockFlash - #endif #endif - ; Set interrupt mode ld a, INT_ON | INT_TIMER1 | INT_LINK out (PORT_INT_MASK), a - ; Set timer frequency (TODO) - - ; Clear RAM - ld hl, 0x8000 - ld (hl), 0 - ld de, 0x8001 - ld bc, 0x7FFF - ldir call formatMem - - ; Set all file handles to unused - ld hl, fileHandleTable - ld (hl), 0xFF - ld de, fileHandleTable + 1 - ld bc, 8 * maxFileStreams - ldir - - ld a, threadRangeMask ; When the first thread is allocated, this will wrap to 0 - ld (lastThreadId), a - -#ifdef COLOR - ; Set GPIO config - ld a, 0xE0 - out (PORT_GPIO_CONFIG), a - ld a, 1 - ld (color_mode), a -#else - ; Initialize LCD - ld a, 1 + LCD_CMD_AUTOINCDEC_SETX - call lcdDelay - out (PORT_LCD_CMD), a ; X-Increment Mode - - ld a, 1 + LCD_CMD_SETOUTPUTMODE - call lcdDelay - out (PORT_LCD_CMD), a ; 8-bit mode - - ld a, 1 + LCD_CMD_SETDISPLAY - call lcdDelay - out (PORT_LCD_CMD), a ; Enable screen - - ld a, 7 + LCD_CMD_POWERSUPPLY_SETLEVEL ; versus +3? TIOS uses +7, and that's the only value that works (the datasheet says go with +3) - call lcdDelay - out (PORT_LCD_CMD), a ; Op-amp control (OPA1) set to max (with DB1 set for some reason) - - ld a, 3 + LCD_CMD_POWERSUPPLY_SETENHANCEMENT ; B - call lcdDelay - out (PORT_LCD_CMD), a ; Op-amp control (OPA2) set to max - - ; Different amounts of contrast look better on different models - #ifdef USB - ld a, 0x2F + LCD_CMD_SETCONTRAST - #else - #ifdef TI73 - ld a, 0x3B + LCD_CMD_SETCONTRAST - #else - ld a, 0x34 + LCD_CMD_SETCONTRAST - #endif - #endif - - ld (currentContrast), a - call lcdDelay - out (PORT_LCD_CMD), a ; Contrast -#endif + call initRandom + call initFilesystem + call initMultitasking + call initDisplay ld de, init call fileExists diff --git a/src/00/debug.asm b/src/00/debug.asm deleted file mode 100644 index fe40bd2..0000000 --- a/src/00/debug.asm +++ /dev/null @@ -1,210 +0,0 @@ -; Kernel debugger -; Access with rst 0x30 -debugger: - push af - push bc - push de - push hl - push ix - push iy - ld ix, 10 - add ix, sp - ld a, i - push af - di - getBankA - push af -debugger_main: - ld iy, debugBuffer - call clearBuffer - - ; Flash status - in a, (2) - bit 2, a - ld a, ' ' - jr z, _ - ld a, 'F' -_: ld de, (96 - 4) << 8 - rst 0x20 - .dw drawChar - - ld de, 0 - ld hl, debugMenu_top - rst 0x20 - .dw drawStr - - ld d, 0x0A - ld hl, debugMenu -_: ld c, (hl) - inc hl - ld b, (hl) - inc hl - inc hl \ inc hl - push hl - ld hl, 0xFFFF - call cpHLBC - jr z, _ - ld h, b \ ld l, c - rst 0x20 - .dw drawStr - ld b, 0x0A - rst 0x20 - .dw newline - pop hl - jr -_ - -_: pop hl - ld de, 0x0606 - ld b, 5 - ld hl, debug_cursorSprite - call putSpriteOR - - call debug_drawRegisters - call fastCopy_skipCheck - - call flushKeys_skipCheck - call waitKey_skipCheck -debug_exit: - pop af - setBankA - pop af - jp po, _ - ei -_: pop iy - pop ix - pop hl - pop de - pop bc - pop af - ret - -debug_kernel: -debug_portmon: -debug_hexEdit: -debug_disassembler: - jp debugger_main - -.macro drawReg(offset) - rst 0x20 - .dw drawStr - push hl - ld l, (ix + offset) - ld h, (ix + offset + 1) - rst 0x20 - .dw drawHexHL - pop hl - inc hl \ inc hl \ inc hl \ inc hl \ inc hl - inc d \ inc d -.endmacro -debug_drawRegisters: - ld de, 41 | (5 << 8) - ld b, 5 - ld hl, debugMenu_regnames - - drawReg(2) ; PC - ; SP - rst 0x20 - .dw drawStr - push hl - push ix \ pop hl - dec hl \ dec hl - rst 0x20 - .dw drawHexHL - pop hl - inc hl \ inc hl \ inc hl \ inc hl \ inc hl - inc d \ inc d - drawReg(-6) ; HL 6 - rst 0x20 - .dw newline - drawReg(-4) ; DE 4 - drawReg(-2) ; BC 2 - drawReg(0) ; AF 0 - rst 0x20 - .dw newline - drawReg(-8) ; IX 8 - drawReg(-10) ; IY 10 - ; IFF2 - rst 0x20 - .dw drawStr - ld a, (ix + -13) - ld hl, debugMenu_iff2reset ; Note: this is intentionally backwards - bit 2, a - jr nz, _ - ld hl, debugMenu_iff2set -_: rst 0x20 - .dw drawStr - ld a, (ix + 0) - call debug_drawFlags - ret - -debug_drawFlags: - ld b, 8 - ld hl, debugMenu_flags -_: bit 0, a - call z, .flagReset - call nz, .flagSet - inc hl - inc d \ inc d - rrca - djnz -_ - ret -.flagSet: - push af - ld a, (hl) - rst 0x20 - .dw drawChar - pop af - ret -.flagReset: - push af - ld a, '-' - rst 0x20 - .dw drawChar - pop af - ret - -debugMenu_top: - .db "Kernel Debugger\n", 0 -debugMenu_regnames: - .db "PC: ", 0 - .db "SP: ", 0 - .db "HL: ", 0 - .db "DE: ", 0 - .db "BC: ", 0 - .db "AF: ", 0 - .db "IX: ", 0 - .db "IY: ", 0 - .db "IFF2: ", 0 -debugMenu_iff2set: - .db "0\nFlags: ", 0 -debugMenu_iff2reset: - .db "1\nFlags: ", 0 -debugMenu_flags: - .db "CNP*H*ZS" -debugMenu: - .dw .return, debug_exit - .dw .hexedit, debug_hexEdit - .dw .dasm, debug_disassembler - .dw .portmon, debug_portmon - .dw .kernel, debug_kernel - .dw 0xFFFF -.hexedit: - .db "Hex Editor", 0 -.dasm: - .db "Disassembler", 0 -.portmon: - .db "Port Monitor", 0 -.kernel: - .db "Kernel State", 0 -.return: - .db "Exit Debugger", 0 -debug_flashLocked: - .db "Flash Locked", 0 -debug_flashUnlocked: - .db "Flash Unlocked", 0 -debug_cursorSprite: - .db 0b10000000 - .db 0b11000000 - .db 0b11100000 - .db 0b11000000 - .db 0b10000000 diff --git a/src/00/display-color.asm b/src/00/display-color.asm index 29723f8..aca577c 100644 --- a/src/00/display-color.asm +++ b/src/00/display-color.asm @@ -29,6 +29,14 @@ colorSupported: #else ; Color screen is 320x240 +initDisplay: + ; Set GPIO config + ld a, 0xE0 + out (PORT_GPIO_CONFIG), a + ld a, 1 + ld (color_mode), a + ret + ;; colorSupported [Color] ;; Sets Z if color is supported on this device. ;; Outputs: diff --git a/src/00/display.asm b/src/00/display.asm index 9dd9029..be6be82 100644 --- a/src/00/display.asm +++ b/src/00/display.asm @@ -1,3 +1,43 @@ +#ifndef COLOR +initDisplay: + ; Initialize LCD + ld a, 1 + LCD_CMD_AUTOINCDEC_SETX + call lcdDelay + out (PORT_LCD_CMD), a ; X-Increment Mode + + ld a, 1 + LCD_CMD_SETOUTPUTMODE + call lcdDelay + out (PORT_LCD_CMD), a ; 8-bit mode + + ld a, 1 + LCD_CMD_SETDISPLAY + call lcdDelay + out (PORT_LCD_CMD), a ; Enable screen + + ld a, 7 + LCD_CMD_POWERSUPPLY_SETLEVEL ; versus +3? TIOS uses +7, and that's the only value that works (the datasheet says go with +3) + call lcdDelay + out (PORT_LCD_CMD), a ; Op-amp control (OPA1) set to max (with DB1 set for some reason) + + ld a, 3 + LCD_CMD_POWERSUPPLY_SETENHANCEMENT ; B + call lcdDelay + out (PORT_LCD_CMD), a ; Op-amp control (OPA2) set to max + + ; Different amounts of contrast look better on different models + #ifdef USB + ld a, 0x2F + LCD_CMD_SETCONTRAST + #else + #ifdef TI73 + ld a, 0x3B + LCD_CMD_SETCONTRAST + #else + ld a, 0x34 + LCD_CMD_SETCONTRAST + #endif + #endif + + ld (currentContrast), a + call lcdDelay + out (PORT_LCD_CMD), a ; Contrast + ret +#endif + ;; clearBuffer [Display] ;; Turns off all pixels on a screen buffer. ;; Inputs: diff --git a/src/00/filestreams.asm b/src/00/filestreams.asm index 44423f8..20c077d 100644 --- a/src/00/filestreams.asm +++ b/src/00/filestreams.asm @@ -1,3 +1,12 @@ +initFilesystem: + ; Set all file handles to unused + ld hl, fileHandleTable + ld (hl), 0xFF + ld de, fileHandleTable + 1 + ld bc, 8 * maxFileStreams + ldir + ret + ;; openFileRead [Filestreams] ;; Opens a file stream in read-only mode. ;; Inputs: @@ -1368,4 +1377,4 @@ _: pop de \ push de seek: cp a ret - \ No newline at end of file + diff --git a/src/00/header.asm b/src/00/header.asm index ceb9c43..677f6d7 100644 --- a/src/00/header.asm +++ b/src/00/header.asm @@ -30,7 +30,7 @@ rlcall: .fill 0x30-$ ; 0x0030 ; RST 0x30 - jp debugger + ret ; unused .fill 0x38-$ ; 0x0038 ; RST 0x38 diff --git a/src/00/jumptable.config b/src/00/jumptable.config index f78021a..75182a8 100644 --- a/src/00/jumptable.config +++ b/src/00/jumptable.config @@ -156,6 +156,7 @@ getKernelShortHash isKernelDirty isAlphaNum + getRandom # Strings strlen @@ -164,4 +165,4 @@ strcmp_sort strchr strtoi - \ No newline at end of file + diff --git a/src/00/random.asm b/src/00/random.asm new file mode 100644 index 0000000..1fecf1d --- /dev/null +++ b/src/00/random.asm @@ -0,0 +1,59 @@ +initRandom: + push hl + push af + setBankA(0x04) ; filesystem, probably pretty unpredictable + ld hl, 0x4000 + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + ld a, r \ rrca \ xor (hl) \ inc hl \ ld (random_seed), a + pop af + pop hl + ret + +;; getRandom [Miscellaneous] +;; Gets an 8-bit random number. +;; Outputs: +;; A: Random number (0-255) +;; Notes: +;; This is not cryptographically random. +getRandom: + push hl + push de + push bc + ld hl, random_seed+4 + ld e, (hl) + inc hl + ld d, (hl) + inc hl + ld c, (hl) + inc hl + ld a, (hl) + ld b, a + rl e \ rl d + rl c \ rla + rl e \ rl d + rl c \ rla + rl e \ rl d + rl c \ rla + ld h, a + rl e \ rl d + rl c \ rla + xor b + rl e \ rl d + xor h + xor c + xor d + ld hl, random_seed+6 + ld de, random_seed+7 + ld bc, 7 + lddr + ld (de), a + pop bc + pop de + pop hl + ret diff --git a/src/00/thread.asm b/src/00/thread.asm index c874309..2930126 100644 --- a/src/00/thread.asm +++ b/src/00/thread.asm @@ -1,3 +1,8 @@ +initMultitasking: + ld a, threadRangeMask ; When the first thread is allocated, this will wrap to 0 + ld (lastThreadId), a + ret + ; Returns the ID of the thread that will launch next getNextThreadID: push hl ; Don't care about the data getThreadEntry provides diff --git a/src/00/util.asm b/src/00/util.asm index 34ac6e9..e342c8e 100644 --- a/src/00/util.asm +++ b/src/00/util.asm @@ -48,6 +48,42 @@ _: ei ret +unprotectRAM: +#ifdef CPU15 + xor a + out (PORT_RAMEXEC_LOWLIMIT), a ; RAM Lower Limit ; out (25), 0 + dec a + out (PORT_RAMEXEC_UPLIMIT), a ; RAM Upper Limit ; out (26), $FF +#else + xor a + out (PORT_RAM_PAGING), a + out (PORT_FLASHEXCLUSION), a + + ld a, 0b000000001 + out (PORT_RAM_PAGING), a + xor a + out (PORT_FLASHEXCLUSION), a +#endif + ret + +unprotectFlash: +#ifdef CPU15 + ld a, 0xFF + out (PORT_FLASHEXEC_LOWLIMIT), a ; Flash Lower Limit + out (PORT_FLASHEXEC_UPLIMIT), a ; Flash Upper Limit +#else + ld a, 0b000000010 + out (PORT_RAM_PAGING), a + xor a + out (PORT_FLASHEXCLUSION), a + + ld a, 0b000000111 + out (PORT_RAM_PAGING), a + xor a + out (PORT_FLASHEXCLUSION), a +#endif + ret + ; TODO: This could use some improvement ;; hexToHL [Miscellaneous] ;; Converts a hexadecimal string to a number. @@ -498,4 +534,3 @@ isAlphaNum: .notLowerAlpha: or a ret - \ No newline at end of file