timer0 #32

Merged
Darkress merged 6 commits from timer0 into main 2023-06-17 18:45:24 +02:00
4 changed files with 55 additions and 14 deletions
Showing only changes of commit dbf6de1027 - Show all commits

View File

@@ -13,17 +13,16 @@ class Main
{ {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
Command[] program = Parser.parser("de/darkress/pic16f84sim/TestPrograms/TPicSim2.LST"); Command[] program = Parser.parser("de/darkress/pic16f84sim/TestPrograms/TPicSim7.LST");
/*for(int i = 0; i < instructions.size(); i++)
{ Memory.initMemory();
program.add(CommandDecoder.decode(instructions.get(i)));
}*/
while(ProgramCounter.getPc() < 1024) while(ProgramCounter.getPc() < 1024)
{ {
program[ProgramCounter.getPc()].execute(); program[ProgramCounter.getPc()].execute();
System.out.println(Memory.workingRegister + " " + Cycles.getCycles()); System.out.println(Integer.toHexString(Memory.workingRegister) + " " + Cycles.getCycles());
System.out.println(Memory.getPCLATH() + " " + Memory.getPCL() + "\n"); System.out.println(Integer.toHexString(Memory.getOption()) + " " + Integer.toHexString(Memory.getTimer()));
System.out.println(Integer.toHexString(Memory.getPCLATH()) + " " + Integer.toHexString(Memory.getPCL()) + "\n");
} }
} }
} }

View File

@@ -5,6 +5,7 @@ public class Cycles {
public static void incCycles() public static void incCycles()
{ {
Timer.increaseTimer();
cycles++; cycles++;
} }

View File

@@ -4,6 +4,13 @@ import java.util.Arrays;
public class Memory public class Memory
{ {
public static void initMemory()
{
Memory.memory[0x81] = 0xFF; //Option
Memory.memory[0x85] = 0x1F; //TrisA
Memory.memory[0x86] = 0xFF; //TrisB
Memory.setRegister(0x03, 0x18); //Status
}
private static final int MEMORY_SIZE = 0xFF; //Addressable Memory private static final int MEMORY_SIZE = 0xFF; //Addressable Memory
public static int workingRegister = 0; public static int workingRegister = 0;
private static final int[] memory = new int[MEMORY_SIZE]; private static final int[] memory = new int[MEMORY_SIZE];
@@ -52,13 +59,18 @@ public class Memory
memory[address] = data; memory[address] = data;
return; return;
} }
if((Arrays.stream(bank1UniqueSpecialRegister).anyMatch(x -> x == address)) && getRegisterBank() == 1) if((Arrays.stream(bank0UniqueSpecialRegister).anyMatch(x -> x == address)) && getRegisterBank() == 1) //bank0
// because of 7 Bit address
{ {
memory[address + 128] = data; memory[address + 128] = data;
return; return;
} }
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 == 0x01) //Reset PrescalerCounter if change on Option or Timer Register
{
Timer.resetTimeToTimerIncrease();
}
if(address == 0x2) //Check if PCL is destination if(address == 0x2) //Check if PCL is destination
{ {
ProgramCounter.loadPc(); ProgramCounter.loadPc();
@@ -79,7 +91,11 @@ 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 if(address == 0x81 || address == 0x01) //Reset PrescalerCounter if change on Option or Timer Register
{
Timer.resetTimeToTimerIncrease();
}
if(address == 0x2 || address == 0x82) //Check if PCL is destination
{ {
ProgramCounter.loadPc(); ProgramCounter.loadPc();
} }
@@ -126,6 +142,16 @@ public class Memory
return memory[0x81]; return memory[0x81];
} }
public static int getTimer()
{
return memory[0x01];
}
public static void setTimer(int data)
{
memory[0x01] = data;
}
public static boolean getZeroBit() public static boolean getZeroBit()
{ {
return (memory[0x03] & 0x04) == 0x04; return (memory[0x03] & 0x04) == 0x04;

View File

@@ -2,7 +2,17 @@ package de.darkress.pic16f84sim.microcontroller;
public class Timer public class Timer
{ {
private static int cyclesToTimerIncrease = getPrescalerFactor(); private static int cyclesToTimerIncrease = 1;
private static boolean timerEnabled()
{
return (Memory.getOption() & 0x20) != 0x20;
}
public static void resetTimeToTimerIncrease()
{
cyclesToTimerIncrease = getPrescalerFactor();
}
private static boolean getPrescalerAsssignment() { private static boolean getPrescalerAsssignment() {
return (Memory.getOption() & 0x08) == 0x08; return (Memory.getOption() & 0x08) == 0x08;
@@ -27,15 +37,20 @@ public class Timer
{ {
System.out.println("Timer Overflow"); System.out.println("Timer Overflow");
} }
Memory.setRegister(0x01, timerRegister); Memory.setTimer(timerRegister);
} }
public static void addToTimer(int increase) public static void increaseTimer()
{ {
cyclesToTimerIncrease -= increase; // TODO: Rethink everything if(!timerEnabled())
{
return;
}
cyclesToTimerIncrease--;
if(cyclesToTimerIncrease == 0) if(cyclesToTimerIncrease == 0)
{ {
cyclesToTimerIncrease = getPrescalerFactor(); resetTimeToTimerIncrease();
increaseTimerRegister();
} }
} }
} }