[go: up one dir, main page]

Menu

[a92f97]: / Cpu.java  Maximize  Restore  History

Download this file

126 lines (114 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
*
* 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);
}
}
}
}
}