Working PC

This commit is contained in:
2024-04-21 13:10:02 +02:00
parent c2f5c3a81a
commit 7be45ed266
3 changed files with 404 additions and 97 deletions

View File

@ -1,8 +1,10 @@
package fabrik.simulator.pic16f84;
public class Commands {
public static void execute(){
;
private static int wRegister = 0;
public static int get_wRegister() {
return wRegister;
}
public static void decode(int instruction){
@ -12,79 +14,20 @@ public class Commands {
int bitaddr = 0x380;
int destinationbit = 0x80;
int d = (instruction & destinationbit) >> 7;
int f = instruction & fileregaddr;
int k = instruction & constant;
int b = (instruction & bitaddr) >> 7;
int j = instruction & jumpaddr;
switch (instruction & 0x3800){
case 0b10000000000000:
System.out.println("CALL");
System.out.println("CALL: " + j);
CALL (j);
return;
case 0b10100000000000:
System.out.println("GOTO");
return;
default:
break;
}
switch (instruction & 0x3F00){
case 0b00011100000000:
System.out.println("ADDWF");
return;
case 0b00010100000000:
System.out.println("ANDWF");
return;
case 0b00100100000000:
System.out.println("COMF");
return;
case 0b00001100000000:
System.out.println("DECF");
return;
case 0b00101100000000:
System.out.println("DECFSZ");
return;
case 0b00101000000000:
System.out.println("INCF");
return;
case 0b00111100000000:
System.out.println("INCFSZ");
return;
case 0b00010000000000:
System.out.println("IORWF");
return;
case 0b00100000000000:
System.out.println("MOVF");
return;
case 0b00110100000000:
System.out.println("RLF");
return;
case 0b00001000000000:
System.out.println("SUBWF");
return;
case 0b00111000000000:
System.out.println("SWAPF");
return;
case 0b00011000000000:
System.out.println("XORWF");
return;
case 0b11100100000000:
System.out.println("ANDLW");
return;
case 0b11101000000000:
System.out.println("XORLW");
return;
case 0b11100000000000:
System.out.println("IORLW");
return;
default:
break;
}
switch (instruction & 0x3F80){
case 0b00000110000000:
System.out.println("CLRF");
return;
case 0b00000100000000:
System.out.println("CLRW");
return;
case 0b00000010000000:
System.out.println("MOVWF");
System.out.println("GOTO: " + j);
GOTO (j);
return;
default:
break;
@ -92,22 +35,28 @@ public class Commands {
switch (instruction & 0x3C00){
case 0b01000000000000:
System.out.println("BCF");
System.out.println("BCF: " + f + " " + b);
DataRegister.clearBit(f, b);
return;
case 0b01010000000000:
System.out.println("BSF");
System.out.println("BSF: " + f + " " + b);
DataRegister.clearBit(f, b);
return;
case 0b01100000000000:
System.out.println("BTFSC");
System.out.println("BTFSC: " + f + " " + b);
BTFSC(f, b);
return;
case 0b01110000000000:
System.out.println("BTFSS");
System.out.println("BTFSS: " + f + " " + b);
BTFSS(f, b);
return;
case 0b11000000000000:
System.out.println("MOVLW");
System.out.println("MOVLW: " + k);
MOVLW (k);
return;
case 0b11010000000000:
System.out.println("RETLW");
System.out.println("RETLW: " + k);
RETLW (k);
return;
default:
break;
@ -115,10 +64,98 @@ public class Commands {
switch (instruction & 0x3E00){
case 0b11111000000000:
System.out.println("ADDLW");
System.out.println("ADDLW: " + k);
ADDLW (k);
return;
case 0b11110000000000:
System.out.println("SUBLW");
System.out.println("SUBLW: " + k);
SUBLW (k);
return;
default:
break;
}
switch (instruction & 0x3F00){
case 0b00011100000000:
System.out.println("ADDWF: " + f + " " + d);
ADDWF (f, d);
return;
case 0b00010100000000:
System.out.println("ANDWF: " + f + " " + d);
ANDWF (f, d);
return;
case 0b00100100000000:
System.out.println("COMF: " + f + " " + d);
COMF (f, d);
return;
case 0b00001100000000:
System.out.println("DECF: " + f + " " + d);
DECF (f, d);
return;
case 0b00101100000000:
System.out.println("DECFSZ: " + f + " " + d);
DECFSZ (f, d);
return;
case 0b00101000000000:
System.out.println("INCF: " + f + " " + d);
INCF (f, d);
return;
case 0b00111100000000:
System.out.println("INCFSZ: " + f + " " + d);
INCFSZ (f, d);
return;
case 0b00010000000000:
System.out.println("IORWF: " + f + " " + d);
IORWF (f, d);
return;
case 0b00100000000000:
System.out.println("MOVF: " + f + " " + d);
MOVF (f, d);
return;
case 0b00110100000000:
System.out.println("RLF: " + f + " " + d);
RLF (f, d);
return;
case 0b00001000000000:
System.out.println("SUBWF: " + f + " " + d);
SUBWF (f, d);
return;
case 0b00111000000000:
System.out.println("SWAPF: " + f + " " + d);
SWAPF (f, d);
return;
case 0b00011000000000:
System.out.println("XORWF: " + f + " " + d);
XORWF (f, d);
return;
case 0b11100100000000:
System.out.println("ANDLW: " + k);
ANDLW (k);
return;
case 0b11101000000000:
System.out.println("XORLW: " + k);
XORLW (k);
return;
case 0b11100000000000:
System.out.println("IORLW: " + k);
IORLW (k);
return;
default:
break;
}
switch (instruction & 0x3F80){
case 0b00000110000000:
System.out.println("CLRF: " + f);
CLRF (f);
return;
case 0b00000100000000:
System.out.println("CLRW");
CLRW ();
return;
case 0b00000010000000:
System.out.println("MOVWF: " + f);
MOVWF (f);
return;
default:
break;
@ -127,15 +164,19 @@ public class Commands {
switch (instruction){
case 0b01100100:
System.out.println("CLRWDT");
//TODO
return;
case 0b1001:
System.out.println("RETFIE");
//TODO
return;
case 0b1000:
System.out.println("RETURN");
//TODO
return;
case 0b01100011:
System.out.println("SLEEP");
//TODO
return;
default:
break;
@ -147,5 +188,215 @@ public class Commands {
else{
System.out.println("Nicht gefunden!");
}
}
public static void GOTO(int jump) {
DataRegister.setPC(jump-1);
}
public static void CALL(int jump) {
//TODO
}
public static void MOVWF(int file) {
DataRegister.setRegister(file, wRegister);
}
public static void CLRW() {
wRegister = 0;
//TODO: Zeroflag
}
public static void CLRF(int file) {
DataRegister.setRegister(file, 0);
//TODO: Zeroflag
}
public static void IORLW(int literal) {
wRegister |= literal;
//TODO: Zeroflag
}
public static void XORLW(int literal) {
wRegister ^= literal;
//TODO: Zeroflag
}
public static void ANDLW(int literal) {
wRegister &= literal;
//TODO: Zeroflag
}
public static void XORWF(int file, int destination) {
int result = wRegister ^ DataRegister.getRegister(file);
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
//TODO: Zeroflag
}
public static void SWAPF(int file, int destination) {
int content = DataRegister.getRegister(file);
int result = (content & 0x0F) << 4 | (content & 0xF0) >> 4;
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
}
public static void SUBWF(int file, int destination) {
int result = (DataRegister.getRegister(file) - wRegister) & 0xFF;
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
// TODO Zeroflag, Carry, DigitCarry
}
public static void RLF(int file, int destination) {
// TODO
}
public static void MOVF(int file, int destination) {
int content = DataRegister.getRegister(file);
if (destination == 0){
wRegister = content;
}
else {
DataRegister.setRegister(file, content);
}
// TODO: Zeroflag
}
public static void IORWF(int file, int destination) {
int result = wRegister | DataRegister.getRegister(file);
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
// TODO: Zeroflag
}
public static void INCFSZ(int file, int destination) {
int result = (DataRegister.getRegister(file) + 1) & 0xFF;
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
if (result == 0) {
DataRegister.increasePC();
}
}
public static void INCF(int file, int destination) {
int result = (DataRegister.getRegister(file) + 1) & 0xFF;
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
//TODO: Zeroflag
}
public static void DECFSZ(int file, int destination) {
int result = (DataRegister.getRegister(file) -1) & 0xFF;
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
if (result == 0){
DataRegister.increasePC();
}
}
public static void DECF(int file, int destination) {
int result = (DataRegister.getRegister(file) -1) & 0xFF;
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
// TODO: Zeroflag
}
private static void COMF(int file, int destination) {
int result = (~ DataRegister.getRegister(file)) & 0xFF;
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
// TODO: Zeroflag
}
public static void ANDWF(int file, int destination) {
int result = wRegister & DataRegister.getRegister(file);
if (destination == 0){
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
// TODO: Zeroflag, Carry, DigitCarry
}
public static void ADDWF(int file, int destination) {
int result = wRegister + DataRegister.getRegister(file);
if (destination == 0) {
wRegister = result;
}
else {
DataRegister.setRegister(file, result);
}
// TODO: Zeroflag, Carry, DigitCarry
}
public static void SUBLW(int literal) {
wRegister = literal - wRegister;
// TODO: Zeroflag, Carry, DigitCarry
}
public static void ADDLW(int literal) {
wRegister += literal;
// TODO: Zeroflag, Carry, DigitCarry
}
public static void RETLW(int literal) {
wRegister = literal;
//TODO: PC vom Stack holen und laden
}
public static void MOVLW(int literal) {
wRegister = literal;
}
public static void BTFSC(int address, int bit) {
if (DataRegister.getBit(address, bit) == 0){
DataRegister.increasePC();
}
}
public static void BTFSS(int address, int bit) {
if (DataRegister.getBit(address, bit) == 1){
DataRegister.increasePC();
}
}
}

View File

@ -10,6 +10,7 @@ import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Controller_FileSelect {
private int [] prog;
@ -32,9 +33,17 @@ public class Controller_FileSelect {
stage.setScene(scene);
stage.show();
System.out.println("\n");
for (int i : prog){
if (i!=0) {
Commands.decode(i);
DataRegister.initDataRegister();
while (true) {
Scanner reader = new Scanner(System.in);
System.out.println("\nWeiter? ");
int n = reader.nextInt();
if (n != 0) {
Commands.decode(prog[DataRegister.getPC()]);
System.out.println("\nPC: " + DataRegister.getPC());
System.out.println("W: " + Commands.get_wRegister());
System.out.printf(Arrays.toString(DataRegister.getDataRegister()));
DataRegister.increasePC();
}
}
}
@ -43,6 +52,4 @@ public class Controller_FileSelect {
public int[] getProg() {
return prog;
}
}

View File

@ -3,25 +3,27 @@ package fabrik.simulator.pic16f84;
import java.util.Arrays;
public class DataRegister {
public static final int STATUS = 0x3;
public static final int TRISA = 0x85;
public static final int TRISB = 0x86;
public static final int PCL = 0x2;
public static final int RP0 = 0x5;
private static int [] dataRegister = new int[0xFF];
private static final int [] dataRegister = new int[0xFF];
public static void initDataRegister() {
dataRegister[0x2] = 0b0;
dataRegister[0x3] = 0b00011000;
dataRegister[PCL] = 0b0;
dataRegister[STATUS] = 0b00011000;
dataRegister[0xA] = 0b0;
dataRegister[0xB] = 0b0;
dataRegister[0x81] = 0b11111111;
dataRegister[0x82] = 0b0;
dataRegister[0x83] = 0b00011000;
dataRegister[0x85] = 0b11111000;
dataRegister[0x86] = 0b11111111;
dataRegister[0x88] = 0b0;
dataRegister[0x8A] = 0b0;
dataRegister[0x8B] = 0b0;
dataRegister[0x82] = dataRegister[PCL];
dataRegister[0x83] = dataRegister[STATUS];
dataRegister[TRISA] = 0b11111000;
dataRegister[TRISB] = 0b11111111;
dataRegister[0x8A] = dataRegister[0xA];
dataRegister[0x8B] = dataRegister[0xB];
System.out.println(Arrays.toString(dataRegister));
}
@ -29,8 +31,55 @@ public class DataRegister {
return dataRegister;
}
private static int bank() {
return ((dataRegister[STATUS] >> RP0) & 1) * 0x80;
}
public static int getRegister(int fileAddress){
return dataRegister[bank() + fileAddress];
}
public static void setRegister(int fileAddress, int content){
dataRegister[bank() + fileAddress] = content;
}
public static int getBit(int address, int bit) {
return (dataRegister[address] >> bit) & 1;
return (dataRegister[bank() + address] >> bit) & 1;
}
public static void clearBit(int address, int bit) {
dataRegister[bank() + address] -= (int) Math.pow(2, bit);
}
public static void setBit(int address, int bit) {
dataRegister[bank() + address] += (int) Math.pow(2, bit);
}
private static int programCounter = 0;
private static void writeToPCL(){
dataRegister[PCL] = programCounter;
dataRegister[0x82] = dataRegister[PCL];
}
public static void increasePC (){
programCounter = getRegister(PCL);
if (programCounter != 0x3FF) {
programCounter++;
}
else {
programCounter = 0;
}
writeToPCL();
}
public static void setPC (int value){
programCounter = value;
writeToPCL();
}
public static int getPC(){
return programCounter;
}
}