-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu_emux.cpp
331 lines (307 loc) · 7.66 KB
/
cpu_emux.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "ansi/c6502.h"
#include "comm.h"
#include "mem.h"
#include "console.h"
#include "ram.h"
#include "state.h"
#include "disassembler.h"
#include "bus.h"
#define qDebug(...)
extern unsigned short gThreadFlags;
extern nc1020_states_t nc1020_states;
static uint64_t& cycles = nc1020_states.cycles;
static uint64_t& last_cycles = nc1020_states.last_cycles;
static uint8_t * rtc_reg=nc1020_states.rtc_reg;
static uint8_t& interr_flag = nc1020_states.interr_flag;
struct BusPC1000 *bus;
extern C6502* cpu;
void cpu_init_emux(){
//assert(use_emux_cpu);
// assert(use_emux_bus);
bus=new BusPC1000();
cpu=new C6502(bus);
bus->cpu=cpu;
// now in resetStates()
//cpu->reset();
}
bool trigger_every_x_ms(int x){
uint32_t target_cycles= CYCLES_SECOND*x/1000;
return (cycles/target_cycles > last_cycles/target_cycles);
}
bool trigger_x_times_per_s(int x){
uint32_t target_cycles=CYCLES_SECOND/x;
return (cycles/target_cycles > last_cycles/target_cycles);
}
void setTime1000() {
const int ADDR_POWER_UP_FLAG = 0x435;
const int ADDR_WATCH_DOG = 0x468;
const int ADDR_IDLESEC = 0x471;
const int ADDR_HOUR = 0x469;
const int ADDR_YEAR = 0x46c;
bus->write(ADDR_IDLESEC, 0); //设置idlesec为0,禁止自动关机
int year = bus->read(ADDR_YEAR) + 1881;
if (year == 2000) {
/*SYSTEMTIME sys;
GetLocalTime(&sys);
bus->write(ADDR_YEAR, sys.wYear - 1881);
bus->write(ADDR_YEAR + 1, sys.wMonth - 1);
bus->write(ADDR_YEAR + 2, sys.wDay - 1);
bus->write(ADDR_HOUR, sys.wHour);
bus->write(ADDR_HOUR + 1, sys.wMinute);
bus->write(ADDR_HOUR + 2, sys.wSecond * 2);*/
}
}
#define TR_s 0x0
#define TR_m 0x01
#define TR_h 0x02
#define TR_d 0x03
#define TR_ms 0x04
#define AR_s 0x05
#define AR_m 0x06
#define AR_h 0x07
#define RTC_CTRL 0x0a
#define INT_CLEAR 0x0b
// nc3000lee1.1 version, only for compare
unsigned char chk_ar0()
{
unsigned char alm=0;
if(!(rtc_reg[RTC_CTRL]&0x02)||!(interr_flag&0x02))
return alm;
if((rtc_reg[AR_h]&0x80))
{
alm=1;
if((rtc_reg[AR_h]&0x1f)!=(rtc_reg[TR_h]&0x1f))
return 0;
}
if((rtc_reg[AR_m]&0x80))
{
alm=1;
if((rtc_reg[AR_m]&0x3f)!=(rtc_reg[TR_m]&0x3f))
return 0;
}
if((rtc_reg[AR_s]&0x80))
{
alm=1;
if((rtc_reg[AR_s]&0x3f)!=(rtc_reg[TR_s]&0x3f))
return 0;
}
return alm;
}
bool chk_ar(){
uint8_t bVar1;
uint8_t uVar2;
if ((rtc_reg[10] >> 1 & 1) == 0) {
return 0;
}
if (((byte)interr_flag >> 1 & 1) == 0) {
return 0;
}
if ((signed char)rtc_reg[7] < (signed char)'\0') {
if (((rtc_reg[2] ^ rtc_reg[7]) & 0x1f) != 0) {
return 0;
}
uVar2 = 1;
bVar1 = rtc_reg[6];
}
else {
uVar2 = 0;
bVar1 = rtc_reg[6];
}
if ((signed char)bVar1 < (signed char)'\0') {
if (((rtc_reg[1] ^ bVar1) & 0x3f) != 0) {
return 0;
}
uVar2 = 1;
bVar1 = rtc_reg[5];
}
else {
bVar1 = rtc_reg[5];
}
if (-1 < (signed char)bVar1) {
return uVar2;
}
if (((rtc_reg[0] ^ bVar1) & 0x3f) == 0) {
return 1;
}
return 0;
}
void setTime3000(){
Store(1025, 0);
rtc_reg[0]++;
if (rtc_reg[0] == '<') {
rtc_reg[0] = '\0';
rtc_reg[1] = rtc_reg[1] + '\x01';
if (rtc_reg[1] == '<') {
rtc_reg[1] = '\0';
rtc_reg[2] = rtc_reg[2] + '\x01';
if (rtc_reg[2] == '\x18') {
rtc_reg[2] = '\0';
rtc_reg[3] = rtc_reg[3] + '\x01';
}
}
}
}
uint8_t trigger256_cnt=0;
int cpu_emux_target_cycles0=128*12;
int cpu_emux_target_cycles=cpu_emux_target_cycles0;
bool time_adjusted;
void sync_time_2000(){
time_t current_time = time(NULL);
struct tm *local_time = localtime(¤t_time);
Store(0x3fa, local_time->tm_year - 103 +0x7a);
Store(0x3fb, local_time->tm_mon);
Store(0x3fc, local_time->tm_mday-1);
//Store(0x3fd, local_time->tm_wday);
rtc_reg[2]=local_time->tm_hour;
rtc_reg[1]=local_time->tm_min;
rtc_reg[0]=local_time->tm_sec;
}
bool soft_reset=0;
void try_soft_reset(){
if(bus->speed_slowdown>512){
soft_reset=1;
printf("soft reset!!\n");
}
}
void cpu_run_emux(){
if(soft_reset){
soft_reset=0;
ram_io[2]=1;
ram_io[3]|=1;
cpu->reset();
//nc1020_states.last_cycles=0;
//nc1020_states.cycles=0;
bus->speed_slowdown=1;
}
//assert(cycles==cpu->getTotalCycles()/12);
char *peeked_msg=peek_message();
if(peeked_msg){
bool need_wait=false;
string cmd=split_s(string(peeked_msg), " ")[0];
//printf("cmd is %s\n",cmd.c_str());
if(cmd=="file_manager"||cmd=="put"||cmd=="get"||cmd=="create_dir"||cmd=="create_dir_hex"){
need_wait=true;
//printf("need wait!!\n");
}
else if(cmd=="log"){
cpu_emux_target_cycles=0;
}
else if(cmd=="nolog"){
cpu_emux_target_cycles=cpu_emux_target_cycles0;
}
if(!need_wait||(cpu->P&4)==0)
{
if(cmd=="file_manager"||cmd=="put"||cmd=="get"){
bus->speed_slowdown=1;
printf("set cks to highest\n");
}
string msg=get_message();
if(!msg.empty()){
handle_cmd(msg);
}
}
}
if(nc2000mode){
if(!time_adjusted && Peek16(0x3fa)==0x7a&&rtc_reg[0]==1){
time_adjusted=1;
sync_time_2000();
}
}
tick++;
if(enable_debug_pc||enable_dyn_debug){
uint8_t & Peek16Debug(uint16_t addr);
unsigned char buf[10];
buf[0]=Peek16Debug(cpu->PC);
buf[1]=Peek16Debug(cpu->PC+1);
buf[2]=Peek16Debug(cpu->PC+2);
buf[3]=0;
printf("tick=%lld ",tick /*, reg_pc*/);
printf("%02x %02x %02x %02x; ",Peek16Debug(cpu->PC), Peek16Debug(cpu->PC+1),Peek16Debug(cpu->PC+2),Peek16Debug(cpu->PC+3));
printf("bs=%02x roa_bbs=%02x ramb=%02x zp=%02x reg=%02x,%02x,%02x,%02x,%03o pc=%s",ram_io[0x00], ram_io[0x0a], ram_io[0x0d], ram_io[0x0f],cpu->A,cpu->X,cpu->Y,cpu->SP,cpu->P,disassemble_next(buf,cpu->PC).c_str());
printf("\n");
//getchar();
}
bool trigger256=trigger_x_times_per_s(256);
if(trigger256){
if(nc1020mode||nc2000mode||nc3000mode){
if(trigger256_cnt==0){
//at the begin of every second
setTime3000();
}
//bump the 1/256 second
rtc_reg[4]=trigger256_cnt;
}
}
//todo study datasheet of how speed affect timers
uint32_t target_cycles=cpu_emux_target_cycles; target_cycles/=bus->speed_slowdown;
uint32_t CpuTicks=cpu->exec2(target_cycles)/12;
CpuTicks*=bus->speed_slowdown;
last_cycles=cycles;
cycles+=CpuTicks;
//magic number to fit timer0 and timer1's code
if(trigger_x_times_per_s(576*50)){
//printf("trigger1!\n");
bus->setTimer();
if(pc1000mode){
if (bus->setTimer0()) {
//timer0用于录放音,蜂鸣器音乐
bus->setIrqTimer0();
//printf("irq1!\n");
cpu->IRQ();
}
if (bus->setTimer1()) {
//timer1用于秒表的百分之一秒,每秒200次
bus->setIrqTimer1();
cpu->IRQ();
//printf("irq2!\n");
}
}
}
if(nc1020mode||nc2000mode||nc3000mode){
bool KeepTimer01( unsigned int cpuTick);
if(KeepTimer01(CpuTicks)){
//adapted from wayback
if ( ram_io[0x04] & 0x0F ) {
//why only set flag for timer0? why not set 0x20
//_ADD_TM1I_BIT() _ADD_TM1I_BIT() is inside KeepTimer01, why set it another time?
//this is from wayback
ram_io[0x01] |= 0x10;
cpu->IRQ();
}
}
}
if(trigger256){
if (bus->timeBaseEnable()) {
//timebase中断为4ms一次,主要用于键盘扫描
bus->setIrqTimeBase();
cpu->IRQ();
}
if(nc1020mode||nc2000mode||nc3000mode){
if(trigger256_cnt%128==0){
if(trigger256_cnt==0&& chk_ar()){
printf("chk_ar() return true!\n");
ram_io[0x3d] = 0x20;
interr_flag&=0xfd;
}else{
if(rtc_reg[10]&1 &&interr_flag&1){
ram_io[0x3d] =0;
cpu->IRQ();
}
}
if (bus->nmiEnable()){
printf("nmi!\n");
cpu->NMI();
}
}
}
}
if(pc1000mode&&trigger_x_times_per_s(2)){
setTime1000();
if (bus->nmiEnable()){
cpu->NMI();
}
}
if(trigger256){
trigger256_cnt++;
}
}