timer0 (#32)
Co-authored-by: Darkress <30271678+DarkressX@users.noreply.github.com> Reviewed-on: darkress/pic16f84-sim#32
This commit was merged in pull request #32.
This commit is contained in:
@@ -3,9 +3,10 @@ package de.darkress.pic16f84sim.microcontroller;
|
||||
public class Cycles {
|
||||
private static int cycles = 0;
|
||||
|
||||
public static void addToCycles(int increase)
|
||||
public static void incCycles()
|
||||
{
|
||||
cycles += increase;
|
||||
Timer.increaseTimer();
|
||||
cycles++;
|
||||
}
|
||||
|
||||
public static int getCycles() {
|
||||
|
||||
@@ -4,6 +4,13 @@ import java.util.Arrays;
|
||||
|
||||
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
|
||||
public static int workingRegister = 0;
|
||||
private static final int[] memory = new int[MEMORY_SIZE];
|
||||
@@ -47,12 +54,19 @@ public class Memory
|
||||
setDataFromIndirectAddress(indirectAddress, data);
|
||||
return;
|
||||
}
|
||||
if(address == 0x01) //Reset PrescalerCounter if change on Option or Timer Register
|
||||
{
|
||||
Timer.resetTimeToTimerIncrease();
|
||||
Timer.setCyclesToTimerIncrease(Timer.getCyclesToTimerIncrease() - 1); //Decrease by one to account for
|
||||
// this command execution
|
||||
}
|
||||
if((Arrays.stream(bank0UniqueSpecialRegister).anyMatch(x -> x == address)) && getRegisterBank() == 0)
|
||||
{
|
||||
memory[address] = data;
|
||||
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;
|
||||
return;
|
||||
@@ -72,6 +86,12 @@ public class Memory
|
||||
|
||||
private static void setDataFromIndirectAddress(int address, int data)
|
||||
{
|
||||
if(address == 0x81 || address == 0x01) //Reset PrescalerCounter if change on Option or Timer Register
|
||||
{
|
||||
Timer.resetTimeToTimerIncrease();
|
||||
Timer.setCyclesToTimerIncrease(Timer.getCyclesToTimerIncrease() - 1); //Decrease by one to account for
|
||||
// this command execution
|
||||
}
|
||||
if((Arrays.stream(bank0UniqueSpecialRegister).anyMatch(x -> x == address)) || (Arrays.stream(bank1UniqueSpecialRegister).anyMatch(x -> x == address)))
|
||||
{
|
||||
memory[address] = data;
|
||||
@@ -79,10 +99,6 @@ public class Memory
|
||||
}
|
||||
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
|
||||
if(address == 0x2) //Check if PCL is destination
|
||||
{
|
||||
ProgramCounter.loadPc();
|
||||
}
|
||||
}
|
||||
|
||||
private static int getRegisterBank()
|
||||
@@ -121,6 +137,21 @@ public class Memory
|
||||
memory[0x8A] = data & 0x1F;
|
||||
}
|
||||
|
||||
public static int getOption()
|
||||
{
|
||||
return memory[0x81];
|
||||
}
|
||||
|
||||
public static int getTimer()
|
||||
{
|
||||
return memory[0x01];
|
||||
}
|
||||
|
||||
public static void setTimer(int data)
|
||||
{
|
||||
memory[0x01] = data;
|
||||
}
|
||||
|
||||
public static boolean getZeroBit()
|
||||
{
|
||||
return (memory[0x03] & 0x04) == 0x04;
|
||||
|
||||
66
de/darkress/pic16f84sim/microcontroller/Timer.java
Normal file
66
de/darkress/pic16f84sim/microcontroller/Timer.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package de.darkress.pic16f84sim.microcontroller;
|
||||
|
||||
public class Timer
|
||||
{
|
||||
public static int getCyclesToTimerIncrease()
|
||||
{
|
||||
return cyclesToTimerIncrease;
|
||||
}
|
||||
|
||||
public static void setCyclesToTimerIncrease(int cyclesToTimerIncrease)
|
||||
{
|
||||
Timer.cyclesToTimerIncrease = cyclesToTimerIncrease;
|
||||
}
|
||||
|
||||
private static int cyclesToTimerIncrease = 1;
|
||||
|
||||
private static boolean timerEnabled()
|
||||
{
|
||||
return (Memory.getOption() & 0x20) != 0x20;
|
||||
}
|
||||
|
||||
public static void resetTimeToTimerIncrease()
|
||||
{
|
||||
cyclesToTimerIncrease = getPrescalerFactor();
|
||||
}
|
||||
|
||||
private static boolean getPrescalerAsssignment() {
|
||||
return (Memory.getOption() & 0x08) == 0x08;
|
||||
}
|
||||
|
||||
private static int getPrescalerFactor() {
|
||||
final int MULTIPLIER = 2;
|
||||
int prescalerPower = Memory.getOption() & 0x07;
|
||||
int prescaler = (int)Math.pow(2, prescalerPower);
|
||||
if(!getPrescalerAsssignment())
|
||||
{
|
||||
return prescaler * MULTIPLIER;
|
||||
}
|
||||
return prescaler;
|
||||
}
|
||||
|
||||
private static void increaseTimerRegister()
|
||||
{
|
||||
int timerRegister = Memory.getRegister(0x01);
|
||||
timerRegister = (timerRegister + 1) % 256;
|
||||
if(timerRegister == 0) //check for timer Overflow --> interrupt
|
||||
{
|
||||
System.out.println("Timer Overflow");
|
||||
}
|
||||
Memory.setTimer(timerRegister);
|
||||
}
|
||||
|
||||
public static void increaseTimer()
|
||||
{
|
||||
if(!timerEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
cyclesToTimerIncrease--;
|
||||
if(cyclesToTimerIncrease == 0)
|
||||
{
|
||||
resetTimeToTimerIncrease();
|
||||
increaseTimerRegister();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user