Fixed bugs. 8 bit prime Sieve demo.

This commit is contained in:
Anachronaut
2024-11-01 20:53:48 -04:00
committed by GitHub
parent 30eebbd9df
commit 2a691ba199
5 changed files with 89 additions and 15 deletions
+17
View File
@@ -131,3 +131,20 @@ DecimalValue:
0x00 ; The ones place. 0x00 ; The ones place.
0x00 ; The tens place. 0x00 ; The tens place.
0x00 ; The hundreds 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
+63
View File
@@ -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
-1
View File
@@ -68,7 +68,6 @@ Instruction instruction_set[] = {
// Input Operations: // Input Operations:
{0xE0, "INA"}, {0xE0, "INA"},
{0xE1, "INB"}, {0xE1, "INB"},
{0xE2, "IND"},
// Special Operations: // Special Operations:
{0xF0, "NOP"}, {0xF0, "NOP"},
{0xFF, "HALT"} {0xFF, "HALT"}
+6 -10
View File
@@ -276,11 +276,12 @@ uint8_t executeOperation(uint8_t Instruction, CPURegisters *cpu) {
break; break;
case 0x33: case 0x33:
// PSHD - Push the Data Pointer Address to the Stack. // PSHD - Push the Data Pointer Address to the Stack.
// Order, low byte, high byte // Order, high byte, low byte
cpu->Data[cpu->StackPointer] = cpu->DataPointer & 0xFF; // This ordering makes it easier to add offsets with register math.
cpu->StackPointer--;
cpu->Data[cpu->StackPointer] = (cpu->DataPointer >> 8) & 0xFF; cpu->Data[cpu->StackPointer] = (cpu->DataPointer >> 8) & 0xFF;
cpu->StackPointer--; cpu->StackPointer--;
cpu->Data[cpu->StackPointer] = cpu->DataPointer & 0xFF;
cpu->StackPointer--;
break; break;
case 0x34: case 0x34:
// POPA - Pop A from the Stack. // POPA - Pop A from the Stack.
@@ -296,9 +297,9 @@ uint8_t executeOperation(uint8_t Instruction, CPURegisters *cpu) {
case 0x36: case 0x36:
// POPD - Pop Data Address from the Stack. // POPD - Pop Data Address from the Stack.
cpu->StackPointer++; cpu->StackPointer++;
cpu->DataPointer = (uint16_t)cpu->Data[cpu->StackPointer] << 8; cpu->DataPointer = (uint16_t)cpu->Data[cpu->StackPointer];
cpu->StackPointer++; cpu->StackPointer++;
cpu->DataPointer |= (uint16_t)cpu->Data[cpu->StackPointer]; cpu->DataPointer |= (uint16_t)cpu->Data[cpu->StackPointer] << 8;
break; break;
// //
// 4x - Data Operations: // 4x - Data Operations:
@@ -381,11 +382,6 @@ uint8_t executeOperation(uint8_t Instruction, CPURegisters *cpu) {
cpu->ProgramCounter++; cpu->ProgramCounter++;
cpu->B = InputHandler(cpu->Program[cpu->ProgramCounter]); cpu->B = InputHandler(cpu->Program[cpu->ProgramCounter]);
break; 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: // Fx - Special Operations:
// //
+3 -4
View File
@@ -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. 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. 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. 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. 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. 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 ### Data Operations: 10 Instructions
Hex Code | Mnemonic | Description 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. 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. 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 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. 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. 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 ### Special Operations: 2 Instructions
Hex Code | Mnemonic | Description Hex Code | Mnemonic | Description