/*
*
* Created on: 25.09.2012
* Author: Christian Wahlmann
* This file is part of achtBit.
* achtBit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* achtBit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with achtBit. If not, see <http://www.gnu.org/licenses/>.
*/
package achtbit;
import java.util.TimerTask;
/**
*
* @author christian
*/
public class Cpu extends TimerTask {
public static int cpuTakt=1000; // in khz
public static boolean screenInterrupt=true;
private int countScreenRefresh;
private int timeRefresh;
private long timeLastMilliseconds;
private long timeRemainingMilliseconds;
private int[] days={
31,28,31,30,31,30,31,31,30,31,30,31
};
public Cpu() {
countScreenRefresh=20;
timeRefresh=10;
timeLastMilliseconds=System.currentTimeMillis();
timeRemainingMilliseconds=0;
}
public void timeTick() {
Memory mem=Memory.instance();
int sec=mem.getByte(ScreenJPanel.timerSec)+1;
if (sec>59) {
sec=0;
int min=mem.getByte(ScreenJPanel.timerMin)+1;
if (min>59) {
min=0;
int hour=mem.getByte(ScreenJPanel.timerHour)+1;
if (hour>23) {
hour=0;
int day=mem.getByte(ScreenJPanel.timerDay);
int month=mem.getByte(ScreenJPanel.timerMonth);
int year=mem.getWord(ScreenJPanel.timerYear);
day++;
int maxDays=days[(month-1)%12];
if (month==2 && year%4==0) maxDays++;
if (day>maxDays) {
day=1;
month++;
if (month>12) {
month=1;
year++;
}
}
mem.setByte(ScreenJPanel.timerDay, day);
mem.setByte(ScreenJPanel.timerMonth, month);
mem.setWord(ScreenJPanel.timerYear, year);
}
mem.setByte(ScreenJPanel.timerHour, hour);
}
mem.setByte(ScreenJPanel.timerMin,min);
}
mem.setByte(ScreenJPanel.timerSec,sec);
}
@Override
public void run() {
if (CpuState.instance().isRunning()) {
timeRefresh--;
if (timeRefresh==0) {
timeRefresh=10;
long t=System.currentTimeMillis();
timeRemainingMilliseconds+=t-timeLastMilliseconds;
timeLastMilliseconds=t;
while (timeRemainingMilliseconds>1000) {
timeRemainingMilliseconds-=1000;
timeTick();
}
}
int i=0;
while (CpuState.instance().isRunning() && i<cpuTakt) {
try {
Command command=new Command();
command.run();
} catch (Exception e) {}
if (CpuState.instance().getState()==CpuState.State.cpuStep) {
CpuState.instance().halt();
}
i++;
}
}
countScreenRefresh--;
if (countScreenRefresh==0) {
countScreenRefresh=20;
Memory.instance().setByte(ScreenJPanel.screenRefresh, 1);
if (CpuState.instance().isRunning()) {
if (screenInterrupt
&& Memory.instance().isEnableInterrupt()) {
Memory.instance().pushWord(Memory.instance().getPC());
Memory.instance().setPC(ScreenJPanel.screenInterrupt);
Memory.instance().setEnableInterrupt(false);
}
}
}
}
}