Compare commits
8 Commits
watchdog
...
timer0Inte
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc53450507 | ||
|
|
791201c884 | ||
|
|
60b288a762 | ||
|
|
101c6ec464 | ||
|
|
4e5f53d46d | ||
|
|
72c377823e | ||
|
|
b408b10cd0 | ||
|
|
9f2a814c34 |
@@ -1,30 +1,30 @@
|
|||||||
package de.darkress.pic16f84sim;
|
package de.darkress.pic16f84sim;
|
||||||
|
|
||||||
|
import de.darkress.pic16f84sim.cli.Cli;
|
||||||
import de.darkress.pic16f84sim.commands.Command;
|
import de.darkress.pic16f84sim.commands.Command;
|
||||||
import de.darkress.pic16f84sim.decoder.CommandDecoder;
|
|
||||||
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;
|
|
||||||
|
|
||||||
class Main
|
class Main
|
||||||
{
|
{
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Command[] program = Parser.parser("de/darkress/pic16f84sim/TestPrograms/TPicSim7.LST");
|
Command[] program = Parser.parser("de/darkress/pic16f84sim/TestPrograms/TPicSim8.LST");
|
||||||
|
|
||||||
Memory.initMemory();
|
Memory.initMemory();
|
||||||
|
|
||||||
while(ProgramCounter.getPc() < 1024)
|
while(ProgramCounter.getPc() < 1024)
|
||||||
{
|
{
|
||||||
System.out.println("Command: " + program[ProgramCounter.getPc()].toString());
|
if((Memory.getRegister(0x03) & 0x10) == 0x00) { // Checking WDT in Status Register
|
||||||
|
System.out.println("Resetting device");
|
||||||
|
Watchdog.resetProgram();
|
||||||
|
}
|
||||||
|
String[] instructionName = program[ProgramCounter.getPc()].getClass().toString().split("\\.");
|
||||||
|
System.out.println("Command: " + instructionName[instructionName.length -1]);
|
||||||
program[ProgramCounter.getPc()].execute();
|
program[ProgramCounter.getPc()].execute();
|
||||||
|
Cli.showRegisters();
|
||||||
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.getPCLATH()) + " " + Integer.toHexString(Memory.getPCL()) + "\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
41
de/darkress/pic16f84sim/cli/Cli.java
Normal file
41
de/darkress/pic16f84sim/cli/Cli.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package de.darkress.pic16f84sim.cli;
|
||||||
|
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Cycles;
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Memory;
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Stack;
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Timer;
|
||||||
|
|
||||||
|
public class Cli
|
||||||
|
{
|
||||||
|
public static void showRegisters() {
|
||||||
|
printf(Memory.workingRegister, "W-Reg", true);
|
||||||
|
printf(Cycles.getCycles(), "Cycles", false);
|
||||||
|
System.out.printf("%n");
|
||||||
|
printf(Memory.getRegister(0x03), "Status", true);
|
||||||
|
printf(Memory.getOption(), "Option", true);
|
||||||
|
System.out.printf("%n");
|
||||||
|
printf(Memory.getFSR(), "FSR", true);
|
||||||
|
printf(Stack.getStackPointer(), "Stackpointer", true);
|
||||||
|
System.out.printf("%n");
|
||||||
|
printf(Memory.getTimer(), "Timer", true);
|
||||||
|
printf(Timer.getCyclesToTimerIncrease(), "Prescaler", true);
|
||||||
|
System.out.printf("%n");
|
||||||
|
printf(Memory.getPCLATH(), "PCLATH", true);
|
||||||
|
printf(Memory.getPCL(), "PCL", true);
|
||||||
|
System.out.printf("%n");
|
||||||
|
System.out.printf("%s:\t\t%s\t", "PortA", Integer.toBinaryString(Memory.getPortA()));
|
||||||
|
System.out.printf("%s:\t\t%s\t", "PortB", Integer.toBinaryString(Memory.getPortB()));
|
||||||
|
System.out.printf("%n");
|
||||||
|
printf(Memory.getRegister(0x0B), "IntCon", true);
|
||||||
|
System.out.printf("%n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printf(int value, String description, boolean hex) {
|
||||||
|
if(hex) {
|
||||||
|
System.out.printf("%s:\t\t%s\t", description, Integer.toHexString(value));
|
||||||
|
} else {
|
||||||
|
System.out.printf("%s:\t\t%d\t", description, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
de/darkress/pic16f84sim/commands/Retfie.java
Normal file
18
de/darkress/pic16f84sim/commands/Retfie.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Cycles;
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Memory;
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
|
||||||
|
import de.darkress.pic16f84sim.microcontroller.Stack;
|
||||||
|
|
||||||
|
public class Retfie extends LiteralCommandUtils implements Command
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void execute()
|
||||||
|
{
|
||||||
|
Cycles.incCycles();
|
||||||
|
Memory.setRegister(0x0B, Memory.getRegister(0x0B) | 0x80); //Set GIE
|
||||||
|
ProgramCounter.setPcFromStack(Stack.pop() + 1);
|
||||||
|
Cycles.incCycles(); // Simulate 2-Cycle Instruction
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -97,12 +97,12 @@ public class CommandDecoder
|
|||||||
|
|
||||||
if (input == 0x0064)
|
if (input == 0x0064)
|
||||||
{
|
{
|
||||||
//clrwdt();
|
return new Clrwdt();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input == 0x0009)
|
if (input == 0x0009)
|
||||||
{
|
{
|
||||||
//retfie();
|
return new Retfie();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input == 0x0008)
|
if (input == 0x0008)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ public class Cycles {
|
|||||||
|
|
||||||
public static void incCycles()
|
public static void incCycles()
|
||||||
{
|
{
|
||||||
Timer.increaseTimer();
|
Timer.decreasePrescaler();
|
||||||
cycles++;
|
cycles++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
de/darkress/pic16f84sim/microcontroller/Interrupt.java
Normal file
13
de/darkress/pic16f84sim/microcontroller/Interrupt.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package de.darkress.pic16f84sim.microcontroller;
|
||||||
|
|
||||||
|
public class Interrupt
|
||||||
|
{
|
||||||
|
private static boolean globalInterruptEnabled() {
|
||||||
|
return (Memory.getRegister(0x0B) & 0x80) == 0x80; // Check GIE
|
||||||
|
|
||||||
|
}
|
||||||
|
public static boolean checkTimerInterruptConditions() {
|
||||||
|
boolean timerInterruptEnabled = (Memory.getRegister(0x0B) & 0x20) == 0x20;
|
||||||
|
return globalInterruptEnabled() && timerInterruptEnabled;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
@@ -110,7 +111,7 @@ public class Memory
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getFSR()
|
public static int getFSR()
|
||||||
{
|
{
|
||||||
return memory[0x4];
|
return memory[0x4];
|
||||||
}
|
}
|
||||||
@@ -142,6 +143,13 @@ public class Memory
|
|||||||
return memory[0x81];
|
return memory[0x81];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int getPortB() {
|
||||||
|
return memory[0x06];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getPortA() {
|
||||||
|
return memory[0x05] & 0x1F;
|
||||||
|
}
|
||||||
public static int getTimer()
|
public static int getTimer()
|
||||||
{
|
{
|
||||||
return memory[0x01];
|
return memory[0x01];
|
||||||
|
|||||||
@@ -36,4 +36,12 @@ public class ProgramCounter
|
|||||||
pc++;
|
pc++;
|
||||||
Memory.setPCL(pc & 0x00FF);
|
Memory.setPCL(pc & 0x00FF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setProgramCounter(int value) {
|
||||||
|
if(value <= 0xFF && value >= 0)
|
||||||
|
{
|
||||||
|
pc = value;
|
||||||
|
Memory.setPCL(pc);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,10 +18,6 @@ public class Stack
|
|||||||
return stack[stackPointer];
|
return stack[stackPointer];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int peek()
|
|
||||||
{
|
|
||||||
return stack[(stackPointer + 7) % 8]; //Get TopOfStack -1 +8 = 7 and modulo 8 to avoid IndexOutOfBound
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void pointNext()
|
private static void pointNext()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -46,21 +46,37 @@ public class Timer
|
|||||||
if(timerRegister == 0) //check for timer Overflow --> interrupt
|
if(timerRegister == 0) //check for timer Overflow --> interrupt
|
||||||
{
|
{
|
||||||
System.out.println("Timer Overflow");
|
System.out.println("Timer Overflow");
|
||||||
|
Memory.setRegister(0x0B, Memory.getRegister(0x0B) | 0x04); //set T0IF
|
||||||
|
if(Interrupt.checkTimerInterruptConditions())
|
||||||
|
{
|
||||||
|
Stack.push(ProgramCounter.getPc());
|
||||||
|
ProgramCounter.setProgramCounter(0x04); // Interrupt Vector
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
de/darkress/pic16f84sim/microcontroller/Watchdog.java
Normal file
37
de/darkress/pic16f84sim/microcontroller/Watchdog.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
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); //Set !T0 in StatusReg
|
||||||
|
ProgramCounter.setProgramCounter(0);
|
||||||
|
watchdogTimer = 18000;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user