watchdog #33
@@ -5,6 +5,7 @@ import de.darkress.pic16f84sim.decoder.CommandDecoder;
|
|||||||
import de.darkress.pic16f84sim.microcontroller.Cycles;
|
import de.darkress.pic16f84sim.microcontroller.Cycles;
|
||||||
import de.darkress.pic16f84sim.microcontroller.Memory;
|
import de.darkress.pic16f84sim.microcontroller.Memory;
|
||||||
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
|
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Watchdog;
|
||||||
import de.darkress.pic16f84sim.parser.Parser;
|
import de.darkress.pic16f84sim.parser.Parser;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -13,18 +14,30 @@ 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/TPicSim7.LST");
|
Command[] program = Parser.parser("de/darkress/pic16f84sim/TestPrograms/TPicSim11.LST");
|
||||||
|
|
||||||
Memory.initMemory();
|
Memory.initMemory();
|
||||||
|
|
||||||
while(ProgramCounter.getPc() < 1024)
|
while(ProgramCounter.getPc() < 1024)
|
||||||
{
|
{
|
||||||
|
if((Memory.getRegister(0x03) & 0x10) == 0x00) { // Checking WDT in Status Register
|
||||||
|
System.out.println("Resetting device");
|
||||||
|
Watchdog.resetProgram();
|
||||||
|
}
|
||||||
System.out.println("Command: " + program[ProgramCounter.getPc()].toString());
|
System.out.println("Command: " + program[ProgramCounter.getPc()].toString());
|
||||||
program[ProgramCounter.getPc()].execute();
|
program[ProgramCounter.getPc()].execute();
|
||||||
|
|
||||||
System.out.println(Integer.toHexString(Memory.workingRegister) + " " + Cycles.getCycles());
|
System.out.println(Integer.toHexString(Memory.workingRegister) + " " + Cycles.getCycles());
|
||||||
System.out.println(Integer.toHexString(Memory.getOption()) + " " + Integer.toHexString(Memory.getTimer()));
|
System.out.println(Integer.toHexString(Memory.getOption()) + " " + Integer.toHexString(Memory.getTimer()));
|
||||||
|
|
||||||
|
System.out.println(Integer.toHexString(Memory.getRegister(0x20)) + " " + Integer.toHexString(Memory.getRegister(0x21)));
|
||||||
|
System.out.println(Integer.toHexString(Memory.getRegister(0x22)) + " " + Integer.toHexString(Memory.getRegister(0x23)));
|
||||||
|
|
||||||
System.out.println(Integer.toHexString(Memory.getPCLATH()) + " " + Integer.toHexString(Memory.getPCL()) + "\n");
|
System.out.println(Integer.toHexString(Memory.getPCLATH()) + " " + Integer.toHexString(Memory.getPCL()) + "\n");
|
||||||
|
|
||||||
|
if(ProgramCounter.getPc() == 0x10) {
|
||||||
|
System.out.println(Memory.getRegister(0x20));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ public class Cycles {
|
|||||||
|
|
||||||
public static void incCycles()
|
public static void incCycles()
|
||||||
{
|
{
|
||||||
Timer.increaseTimer();
|
Timer.decreasePrescaler();
|
||||||
cycles++;
|
cycles++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ public class Memory
|
|||||||
setDataFromIndirectAddress(indirectAddress, data);
|
setDataFromIndirectAddress(indirectAddress, data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(address == 0x01) //Reset PrescalerCounter if change on Option or Timer Register
|
if(address == 0x01 && !Timer.getPrescalerAssignment()) //Reset PrescalerCounter if change on Option or Timer
|
||||||
|
// Register
|
||||||
{
|
{
|
||||||
Timer.resetTimeToTimerIncrease();
|
Timer.resetTimeToTimerIncrease();
|
||||||
Timer.setCyclesToTimerIncrease(Timer.getCyclesToTimerIncrease() - 1); //Decrease by one to account for
|
Timer.setCyclesToTimerIncrease(Timer.getCyclesToTimerIncrease() - 1); //Decrease by one to account for
|
||||||
@@ -86,7 +87,7 @@ public class Memory
|
|||||||
|
|
||||||
private static void setDataFromIndirectAddress(int address, int data)
|
private static void setDataFromIndirectAddress(int address, int data)
|
||||||
{
|
{
|
||||||
if(address == 0x81 || address == 0x01) //Reset PrescalerCounter if change on Option or Timer Register
|
if((address == 0x81 || address == 0x01) && !Timer.getPrescalerAssignment()) //Reset PrescalerCounter if change on Option or Timer Register
|
||||||
{
|
{
|
||||||
Timer.resetTimeToTimerIncrease();
|
Timer.resetTimeToTimerIncrease();
|
||||||
Timer.setCyclesToTimerIncrease(Timer.getCyclesToTimerIncrease() - 1); //Decrease by one to account for
|
Timer.setCyclesToTimerIncrease(Timer.getCyclesToTimerIncrease() - 1); //Decrease by one to account for
|
||||||
|
|||||||
@@ -36,4 +36,9 @@ public class ProgramCounter
|
|||||||
pc++;
|
pc++;
|
||||||
Memory.setPCL(pc & 0x00FF);
|
Memory.setPCL(pc & 0x00FF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void resetProgramCounter() {
|
||||||
|
pc = 0;
|
||||||
|
Memory.setPCL(0x0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class Timer
|
|||||||
cyclesToTimerIncrease = getPrescalerFactor();
|
cyclesToTimerIncrease = getPrescalerFactor();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean getPrescalerAsssignment() {
|
public static boolean getPrescalerAssignment() {
|
||||||
return (Memory.getOption() & 0x08) == 0x08;
|
return (Memory.getOption() & 0x08) == 0x08;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ public class Timer
|
|||||||
final int MULTIPLIER = 2;
|
final int MULTIPLIER = 2;
|
||||||
int prescalerPower = Memory.getOption() & 0x07;
|
int prescalerPower = Memory.getOption() & 0x07;
|
||||||
int prescaler = (int)Math.pow(2, prescalerPower);
|
int prescaler = (int)Math.pow(2, prescalerPower);
|
||||||
if(!getPrescalerAsssignment())
|
if(!getPrescalerAssignment())
|
||||||
{
|
{
|
||||||
return prescaler * MULTIPLIER;
|
return prescaler * MULTIPLIER;
|
||||||
}
|
}
|
||||||
@@ -50,17 +50,27 @@ public class Timer
|
|||||||
Memory.setTimer(timerRegister);
|
Memory.setTimer(timerRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void increaseTimer()
|
public static void decreasePrescaler()
|
||||||
{
|
{
|
||||||
if(!timerEnabled())
|
if(getPrescalerAssignment()) { // Assigned to WatchdogTimer
|
||||||
{
|
cyclesToTimerIncrease--;
|
||||||
return;
|
if(cyclesToTimerIncrease == 0) {
|
||||||
|
resetTimeToTimerIncrease();
|
||||||
|
Watchdog.decreaseWatchdogTimer();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Watchdog.decreaseWatchdogTimer();
|
||||||
}
|
}
|
||||||
cyclesToTimerIncrease--;
|
if(timerEnabled()) {
|
||||||
if(cyclesToTimerIncrease == 0)
|
if(!getPrescalerAssignment()) { // Assigned to timer0
|
||||||
{
|
cyclesToTimerIncrease--;
|
||||||
resetTimeToTimerIncrease();
|
if(cyclesToTimerIncrease == 0) {
|
||||||
increaseTimerRegister();
|
resetTimeToTimerIncrease();
|
||||||
|
Timer.increaseTimerRegister();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Timer.increaseTimerRegister();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
32
de/darkress/pic16f84sim/microcontroller/Watchdog.java
Normal file
32
de/darkress/pic16f84sim/microcontroller/Watchdog.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package de.darkress.pic16f84sim.microcontroller;
|
||||||
|
|
||||||
|
public class Watchdog
|
||||||
|
{
|
||||||
|
private static int watchdogTimer = 18000;
|
||||||
|
public static int getWatchdogTimer()
|
||||||
|
{
|
||||||
|
return watchdogTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void decreaseWatchdogTimer()
|
||||||
|
{
|
||||||
|
watchdogTimer--;
|
||||||
|
checkWatchdog();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void checkWatchdog()
|
||||||
|
{
|
||||||
|
if(watchdogTimer == 0)
|
||||||
|
{
|
||||||
|
System.out.println("Watchdog Time-Out");
|
||||||
|
Memory.setRegister(0x03, Memory.getRegister(0x03) & 0xEF); //Clear !TO in StatusReg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void resetProgram()
|
||||||
|
{
|
||||||
|
Memory.setRegister(0x03, Memory.getRegister(0x03) | 0x10);
|
||||||
|
watchdogTimer = 18000;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user