Compare commits
3 Commits
timer0
...
clrwdtInst
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02f7e52856 | ||
|
|
9f2a814c34 | ||
|
|
f2423d0d71 |
@@ -5,6 +5,7 @@ import de.darkress.pic16f84sim.decoder.CommandDecoder;
|
||||
import de.darkress.pic16f84sim.microcontroller.Cycles;
|
||||
import de.darkress.pic16f84sim.microcontroller.Memory;
|
||||
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
|
||||
import de.darkress.pic16f84sim.microcontroller.Watchdog;
|
||||
import de.darkress.pic16f84sim.parser.Parser;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -13,18 +14,30 @@ class Main
|
||||
{
|
||||
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();
|
||||
|
||||
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());
|
||||
program[ProgramCounter.getPc()].execute();
|
||||
|
||||
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.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");
|
||||
|
||||
if(ProgramCounter.getPc() == 0x10) {
|
||||
System.out.println(Memory.getRegister(0x20));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
24
de/darkress/pic16f84sim/commands/Clrwdt.java
Normal file
24
de/darkress/pic16f84sim/commands/Clrwdt.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package de.darkress.pic16f84sim.commands;
|
||||
|
||||
import de.darkress.pic16f84sim.microcontroller.*;
|
||||
|
||||
public class Clrwdt extends LiteralCommandUtils implements Command
|
||||
{
|
||||
|
||||
public Clrwdt()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute()
|
||||
{
|
||||
ProgramCounter.incPC();
|
||||
Cycles.incCycles();
|
||||
|
||||
Memory.setRegister(0x03, Memory.getRegister(0x03) | 0x18);
|
||||
if(Timer.getPrescalerAssignment()) {
|
||||
Timer.resetTimeToTimerIncrease();
|
||||
Watchdog.resetWatchdogTimer();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,7 +97,7 @@ public class CommandDecoder
|
||||
|
||||
if (input == 0x0064)
|
||||
{
|
||||
//clrwdt();
|
||||
return new Clrwdt();
|
||||
}
|
||||
|
||||
if (input == 0x0009)
|
||||
|
||||
@@ -5,7 +5,7 @@ public class Cycles {
|
||||
|
||||
public static void incCycles()
|
||||
{
|
||||
Timer.increaseTimer();
|
||||
Timer.decreasePrescaler();
|
||||
cycles++;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,8 @@ public class Memory
|
||||
setDataFromIndirectAddress(indirectAddress, data);
|
||||
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.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)
|
||||
{
|
||||
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.setCyclesToTimerIncrease(Timer.getCyclesToTimerIncrease() - 1); //Decrease by one to account for
|
||||
|
||||
@@ -36,4 +36,9 @@ public class ProgramCounter
|
||||
pc++;
|
||||
Memory.setPCL(pc & 0x00FF);
|
||||
}
|
||||
|
||||
public static void resetProgramCounter() {
|
||||
pc = 0;
|
||||
Memory.setPCL(0x0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class Timer
|
||||
cyclesToTimerIncrease = getPrescalerFactor();
|
||||
}
|
||||
|
||||
private static boolean getPrescalerAsssignment() {
|
||||
public static boolean getPrescalerAssignment() {
|
||||
return (Memory.getOption() & 0x08) == 0x08;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class Timer
|
||||
final int MULTIPLIER = 2;
|
||||
int prescalerPower = Memory.getOption() & 0x07;
|
||||
int prescaler = (int)Math.pow(2, prescalerPower);
|
||||
if(!getPrescalerAsssignment())
|
||||
if(!getPrescalerAssignment())
|
||||
{
|
||||
return prescaler * MULTIPLIER;
|
||||
}
|
||||
@@ -50,17 +50,27 @@ public class Timer
|
||||
Memory.setTimer(timerRegister);
|
||||
}
|
||||
|
||||
public static void increaseTimer()
|
||||
public static void decreasePrescaler()
|
||||
{
|
||||
if(!timerEnabled())
|
||||
{
|
||||
return;
|
||||
if(getPrescalerAssignment()) { // Assigned to WatchdogTimer
|
||||
cyclesToTimerIncrease--;
|
||||
if(cyclesToTimerIncrease == 0) {
|
||||
resetTimeToTimerIncrease();
|
||||
Watchdog.decreaseWatchdogTimer();
|
||||
}
|
||||
} else {
|
||||
Watchdog.decreaseWatchdogTimer();
|
||||
}
|
||||
cyclesToTimerIncrease--;
|
||||
if(cyclesToTimerIncrease == 0)
|
||||
{
|
||||
resetTimeToTimerIncrease();
|
||||
increaseTimerRegister();
|
||||
if(timerEnabled()) {
|
||||
if(!getPrescalerAssignment()) { // Assigned to timer0
|
||||
cyclesToTimerIncrease--;
|
||||
if(cyclesToTimerIncrease == 0) {
|
||||
resetTimeToTimerIncrease();
|
||||
Timer.increaseTimerRegister();
|
||||
}
|
||||
} else {
|
||||
Timer.increaseTimerRegister();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
36
de/darkress/pic16f84sim/microcontroller/Watchdog.java
Normal file
36
de/darkress/pic16f84sim/microcontroller/Watchdog.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package de.darkress.pic16f84sim.microcontroller;
|
||||
|
||||
public class Watchdog
|
||||
{
|
||||
private static int watchdogTimer = 18000;
|
||||
public static int getWatchdogTimer()
|
||||
{
|
||||
return watchdogTimer;
|
||||
}
|
||||
|
||||
public static void resetWatchdogTimer() {
|
||||
watchdogTimer = 18000;
|
||||
}
|
||||
|
||||
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