83 lines
2.8 KiB
Java
83 lines
2.8 KiB
Java
package fabrik.simulator.pic16f84;
|
|
|
|
import fabrik.simulator.pic16f84.interfaces.DataRegisterInterface;
|
|
import fabrik.simulator.pic16f84.interfaces.FrontendControllerInterface;
|
|
import fabrik.simulator.pic16f84.interfaces.InterruptInterface;
|
|
import fabrik.simulator.pic16f84.interfaces.TimerInterface;
|
|
|
|
public class Timer implements TimerInterface {
|
|
private final int TIMERREG = 0x1;
|
|
private final int T0SE = 0x4;
|
|
private final int T0CS = 0x5;
|
|
private final int OPTION = 0x81;
|
|
|
|
private int oldpin = 0;
|
|
|
|
private final DataRegisterInterface dataRegister;
|
|
private final InterruptInterface interrupts;
|
|
private final PreScaler preScaler;
|
|
private final FrontendControllerInterface frontendController;
|
|
|
|
public Timer (DataRegisterInterface dataRegister, InterruptInterface interrupts, PreScaler preScaler, FrontendControllerInterface frontendController){
|
|
this.dataRegister = dataRegister;
|
|
this.interrupts = interrupts;
|
|
this.preScaler = preScaler;
|
|
this.frontendController = frontendController;
|
|
}
|
|
|
|
public void cycles(int cycles){
|
|
if (dataRegister.getDirectBit(OPTION, T0CS) == 0){
|
|
if (preScaler.isPrescalerOnTimer()){
|
|
for (int i = 0; i < cycles; i++){
|
|
preScaler.decrement(false);
|
|
}
|
|
} else {
|
|
increment(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void incrementFromPin (int register){
|
|
if ((dataRegister.getDirectBit(OPTION, T0CS) == 1) && !frontendController.isSleeping()){
|
|
int newpin = (register >> 4) & 1;
|
|
if (newpin != oldpin) {
|
|
if (dataRegister.getDirectBit(OPTION, T0SE) == 0) {
|
|
// Low to high
|
|
if (newpin == 1 && oldpin == 0) {
|
|
if (preScaler.isPrescalerOnTimer())
|
|
preScaler.decrement(true);
|
|
else
|
|
increment(true);
|
|
}
|
|
} else {
|
|
// High to low
|
|
if (newpin == 0 && oldpin == 1) {
|
|
if (preScaler.isPrescalerOnTimer())
|
|
preScaler.decrement(true);
|
|
else
|
|
increment(true);
|
|
}
|
|
}
|
|
oldpin = newpin;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void increment(boolean manual) {
|
|
int timer = dataRegister.getDirectRegister(TIMERREG);
|
|
timer++;
|
|
if (timer > 0xFF){
|
|
interrupts.triggerTMR0(manual);
|
|
dataRegister.setDirectRegister(TIMERREG, 0);
|
|
} else {
|
|
dataRegister.setDirectRegister(1, timer);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void initialize(PICComponents picComponents) {
|
|
|
|
}
|
|
}
|