From 2a691ba19923c2b21e120b06365699c274defb8f Mon Sep 17 00:00:00 2001 From: Anachronaut <75824887+RealBusinessAccount@users.noreply.github.com> Date: Fri, 1 Nov 2024 20:53:48 -0400 Subject: [PATCH] Fixed bugs. 8 bit prime Sieve demo. --- Programs/Libraries/print.asm | 17 +++++++++ Programs/primeSieve/8bitSieve.asm | 63 +++++++++++++++++++++++++++++++ Source/Assembler/assembly.c | 1 - Source/Emulator/cpu.c | 16 +++----- SplitBit Programming Manual.md | 7 ++-- 5 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 Programs/primeSieve/8bitSieve.asm diff --git a/Programs/Libraries/print.asm b/Programs/Libraries/print.asm index b898ccd..468647a 100644 --- a/Programs/Libraries/print.asm +++ b/Programs/Libraries/print.asm @@ -131,3 +131,20 @@ DecimalValue: 0x00 ; The ones place. 0x00 ; The tens place. 0x00 ; The hundreds place. + +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 diff --git a/Programs/primeSieve/8bitSieve.asm b/Programs/primeSieve/8bitSieve.asm new file mode 100644 index 0000000..cba2716 --- /dev/null +++ b/Programs/primeSieve/8bitSieve.asm @@ -0,0 +1,63 @@ +; This is an implementation of The Sieve of Eratosthenes that finds all the primes between 2 and 255. + +#Include ../Libraries/print.asm + +#Program + +start: + ; Search the list until we find a prime. + SETD DataTop + CCF ; Clear the carry flag. In later cycles, the carry flag will be set at the end of the next loop. We'll want it cleared. + INIA 0x00 + INIB 0x00 + findPrimeLoop: + LDB ; Load an element into B. + BRB foundPrime ; If it's zero, it's a prime. + INCA ; Increment A, our index. + INCD ; Increment the Data Pointer. + BRA end ; If A becomes zero, we've looked through the whole list without finding another prime. + BRI findPrimeLoop ; Keep searching for the next prime. + foundPrime: + ; If we've found a prime, we should print it and mark it off the list so we don't print it again. + CALL printByteDecimal ; A contains our prime, so we can just call the print subroutine. + CALL blankSpace ; Put a space afterward to keep things easy to read. + INIB 0x01 ; Set B to 1. + STB ; Mark this prime off the list. + markMultiples: + ; Now, we mark each multiple of this prime as nonprime until we reach the end of the list. + PSHD ; Save the Data Pointer to the stack. + POPB ; Pop its low byte into B. + ADD ; Add them together. + PSHQ ; Store the result back onto the stack. + POPD ; Pop the modified address into the Data Pointer. + INIB 0x01 ; Set B to 1. + STB ; Store B to mark the value as nonprime. + BRC start ; If the previous add overflowed, the next nonprime is outside the range of our list, so start over with a new prime. + BRI markMultiples ; Otherwise, loop again to mark the next multiple as nonprime. + end: + CALL lineFeed ; Print a linefeed to make it look nice. + HALT ; The program is done, we found all the primes! + + + + +#Data + +; The table of our prime candidates. +DataTop: +0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 diff --git a/Source/Assembler/assembly.c b/Source/Assembler/assembly.c index 8447712..6b535c6 100644 --- a/Source/Assembler/assembly.c +++ b/Source/Assembler/assembly.c @@ -68,7 +68,6 @@ Instruction instruction_set[] = { // Input Operations: {0xE0, "INA"}, {0xE1, "INB"}, - {0xE2, "IND"}, // Special Operations: {0xF0, "NOP"}, {0xFF, "HALT"} diff --git a/Source/Emulator/cpu.c b/Source/Emulator/cpu.c index 1d3a4ad..7f60232 100644 --- a/Source/Emulator/cpu.c +++ b/Source/Emulator/cpu.c @@ -276,11 +276,12 @@ uint8_t executeOperation(uint8_t Instruction, CPURegisters *cpu) { break; case 0x33: // PSHD - Push the Data Pointer Address to the Stack. - // Order, low byte, high byte - cpu->Data[cpu->StackPointer] = cpu->DataPointer & 0xFF; - cpu->StackPointer--; + // Order, high byte, low byte + // This ordering makes it easier to add offsets with register math. cpu->Data[cpu->StackPointer] = (cpu->DataPointer >> 8) & 0xFF; cpu->StackPointer--; + cpu->Data[cpu->StackPointer] = cpu->DataPointer & 0xFF; + cpu->StackPointer--; break; case 0x34: // POPA - Pop A from the Stack. @@ -296,9 +297,9 @@ uint8_t executeOperation(uint8_t Instruction, CPURegisters *cpu) { case 0x36: // POPD - Pop Data Address from the Stack. cpu->StackPointer++; - cpu->DataPointer = (uint16_t)cpu->Data[cpu->StackPointer] << 8; + cpu->DataPointer = (uint16_t)cpu->Data[cpu->StackPointer]; cpu->StackPointer++; - cpu->DataPointer |= (uint16_t)cpu->Data[cpu->StackPointer]; + cpu->DataPointer |= (uint16_t)cpu->Data[cpu->StackPointer] << 8; break; // // 4x - Data Operations: @@ -381,11 +382,6 @@ uint8_t executeOperation(uint8_t Instruction, CPURegisters *cpu) { cpu->ProgramCounter++; cpu->B = InputHandler(cpu->Program[cpu->ProgramCounter]); break; - case 0xE2: - // IND - Read an Input to Data Memory. - cpu->ProgramCounter++; - cpu->Data[cpu->DataPointer] = InputHandler(cpu->Program[cpu->ProgramCounter]); - break; // // Fx - Special Operations: // diff --git a/SplitBit Programming Manual.md b/SplitBit Programming Manual.md index 9c3ff6c..2d33fe4 100644 --- a/SplitBit Programming Manual.md +++ b/SplitBit Programming Manual.md @@ -62,10 +62,10 @@ Hex Code | Mnemonic | Description 30 | PSHQ | Stores Q into Data Memory at the location referenced by the Stack Pointer then decrements the Stack Pointer. 31 | PSHA | Stores A into Data Memory at the location referenced by the Stack Pointer then decrements the Stack Pointer. 32 | PSHB | Stores B into Data Memory at the location referenced by the Stack Pointer then decrements the Stack Pointer. -33 | PSHD | Stores the Data Pointer to the next two bytes in the stack, decrements the Stack Pointer by two. +33 | PSHD | Stores the Data Pointer to the stack, with the low byte on top. Increments the Stack Pointer by two. 34 | POPA | Reads the location referenced by the Stack Pointer from Data Memory into A then increments the Stack Pointer. 35 | POPB | Reads the location referenced by the Stack Pointer from Data Memory into B then increments the Stack Pointer. -36 | POPD | Restores the Program Counter from the top two bytes in the stack, increments the Stack Pointer by two. +36 | POPD | Restores the Program Counter from the stack, increments the Stack Pointer by two. ### Data Operations: 10 Instructions Hex Code | Mnemonic | Description @@ -89,12 +89,11 @@ D0 | OUTQ | Writes the value of Q to an Output specified by the next byte of Pro D1 | OUTA | Writes the value of A to an Output specified by the next byte of Program Memory. D2 | OUTB | Writes the value of B to an Output specified by the next byte of Program Memory. -### Input Operations: 3 Instructions +### Input Operations: 2 Instructions Hex Code | Mnemonic | Description -- | -- | -- E0 | INA | Writes the value of an Input to A. The input port is specified the next byte of Program Memory. E1 | INB | Writes the value of an Input to B. The input port is specified the next byte of Program Memory. -E2 | IND | Writes the value of an Input to Data Memory at the location referenced by the Data Pointer. The input port is specified by the next byte of Program Memory. ### Special Operations: 2 Instructions Hex Code | Mnemonic | Description