Fixed bugs. 8 bit prime Sieve demo.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -68,7 +68,6 @@ Instruction instruction_set[] = {
|
||||
// Input Operations:
|
||||
{0xE0, "INA"},
|
||||
{0xE1, "INB"},
|
||||
{0xE2, "IND"},
|
||||
// Special Operations:
|
||||
{0xF0, "NOP"},
|
||||
{0xFF, "HALT"}
|
||||
|
||||
+6
-10
@@ -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:
|
||||
//
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user