Compare commits

..

1 Commits

Author SHA1 Message Date
Darkress
e58624dacd implemented Goto Instruction; Increment ProgramCounter; 2023-05-29 18:05:26 +02:00
9 changed files with 32 additions and 92 deletions

View File

@@ -12,14 +12,14 @@ class Main
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Command> program = new ArrayList<>(); ArrayList<Command> program = new ArrayList<>();
int input1 = 0x27FF; Memory.workingRegister = 0xAA;
program.add(CommandDecoder.decode(0x3011)); int input1 = 0x2FFF;
program.add(CommandDecoder.decode(0x2003)); program.add(CommandDecoder.decode(input1));
program.add(CommandDecoder.decode(0x3022));
program.add(CommandDecoder.decode(0x0008)); Memory.setPCLATH(0xFF);
while(true) program.get(0).execute();
{
program.get(ProgramCounter.getPc()).execute(); //ProgramCounter.incPC();
}
} }
} }

View File

@@ -1,21 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
import de.darkress.pic16f84sim.microcontroller.Stack;
public class Call extends CommandUtils implements Command
{
private final int literal;
public Call(int input)
{
literal = input & 0x07FF;
}
@Override
public void execute()
{
Stack.push(ProgramCounter.getPc() + 1);
ProgramCounter.setPcFrom11BitLiteral(literal);
}
}

View File

@@ -1,5 +1,6 @@
package de.darkress.pic16f84sim.commands; package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter; import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Goto extends CommandUtils implements Command public class Goto extends CommandUtils implements Command
@@ -14,6 +15,6 @@ public class Goto extends CommandUtils implements Command
@Override @Override
public void execute() public void execute()
{ {
ProgramCounter.setPcFrom11BitLiteral(literal); ProgramCounter.setPcForGotoCall(literal);
} }
} }

View File

@@ -1,22 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
import de.darkress.pic16f84sim.microcontroller.Stack;
public class Retlw extends CommandUtils implements Command
{
private final int literal;
public Retlw(int input)
{
literal = input & 0x00FF;
}
@Override
public void execute()
{
Memory.workingRegister = literal;
ProgramCounter.setPcFromStack(Stack.pop());
}
}

View File

@@ -1,13 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
import de.darkress.pic16f84sim.microcontroller.Stack;
public class Return extends CommandUtils implements Command
{
@Override
public void execute()
{
ProgramCounter.setPcFromStack(Stack.pop());
}
}

View File

@@ -100,7 +100,8 @@ public class CommandDecoder
switch(input & 0x3800) switch(input & 0x3800)
{ {
case 0x2000: case 0x2000:
return new Call(input); //call();
break;
case 0x2800: case 0x2800:
return new Goto(input); return new Goto(input);
} }
@@ -110,7 +111,8 @@ public class CommandDecoder
case 0x3000: case 0x3000:
return new Movlw(input); return new Movlw(input);
case 0x3400: case 0x3400:
return new Retlw(input); //retlw();
break;
} }
if ((input | 0x0060) == 0x0060) if ((input | 0x0060) == 0x0060)
@@ -130,7 +132,8 @@ public class CommandDecoder
if (input == 0x0008) if (input == 0x0008)
{ {
return new Return(); //return();
//This is the function name. Do not mistake this for a normal return!
} }
if (input == 0x0063) if (input == 0x0063)

View File

@@ -59,10 +59,6 @@ public class Memory
} }
memory[address] = data; memory[address] = data;
memory[address + 128] = data; //Ensure data is written to both banks to simulate mapping memory[address + 128] = data; //Ensure data is written to both banks to simulate mapping
if(address == 0x2) //Check if PCL is destination
{
ProgramCounter.loadPc();
}
} }
private static int getDataFromIndirectAddress(int address) private static int getDataFromIndirectAddress(int address)
@@ -79,10 +75,6 @@ public class Memory
} }
memory[address % 128] = data; // else: Registers.Registers which are mapped memory[address % 128] = data; // else: Registers.Registers which are mapped
memory[address % 128 + 128] = data; //Ensure data is written to both banks to simulate mapping memory[address % 128 + 128] = data; //Ensure data is written to both banks to simulate mapping
if(address == 0x2) //Check if PCL is destination
{
ProgramCounter.loadPc();
}
} }
private static int getRegisterBank() private static int getRegisterBank()

View File

@@ -11,7 +11,7 @@ public class ProgramCounter
return pc; return pc;
} }
public static void setPcFrom11BitLiteral(int data) public static void setPcForGotoCall(int data)
{ {
int pcl = data & 0x00FF; int pcl = data & 0x00FF;
int pch = Memory.getPCLATH(); int pch = Memory.getPCLATH();
@@ -20,20 +20,19 @@ public class ProgramCounter
pc = (pch) + pcl; pc = (pch) + pcl;
} }
public static void setPcFromStack(int stack)
{
pc = stack;
Memory.setPCL(pc & 0x00FF);
}
public static void loadPc()
{
pc = (Memory.getPCLATH() <<8) + Memory.getPCL();
}
public static void incPC() //is called after every instruction execution public static void incPC() //is called after every instruction execution
{ {
pc++; int pcl = Memory.getPCL();
Memory.setPCL(pc & 0x00FF); int pclath = Memory.getPCLATH();
if(pcl == 0xFF)
{
pclath++;
pcl = 0;
} else {
pcl++;
}
Memory.setPCL(pcl);
Memory.setPCLATH(pclath);
pc = (pclath <<8) + pcl;
} }
} }

View File

@@ -14,8 +14,9 @@ public class Stack
public static int pop() public static int pop()
{ {
int tmp = stack[stackPointer];
pointPrevious(); pointPrevious();
return stack[stackPointer]; return tmp;
} }
public static int peek() public static int peek()