-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhardware.cpp
More file actions
175 lines (147 loc) · 6.2 KB
/
hardware.cpp
File metadata and controls
175 lines (147 loc) · 6.2 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
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
// *******************************************************************************************************************************
// *******************************************************************************************************************************
//
// Name: hardware.c
// Purpose: Hardware Emulation
// Created: 29th June 2022
// Author: Paul Robson ([email protected])
//
// *******************************************************************************************************************************
// *******************************************************************************************************************************
#include "sys_processor.h"
#include "hardware.h"
#include "gfx.h"
#include <stdio.h>
#include <stdlib.h>
#define QSIZE (10)
struct _Queue {
int count;
int queue[QSIZE];
};
struct _Queue keyboardQueue;
static int SN76489_reg[8]; // 8 registers of 76489 (Tone/Attenuation 0..3)
static int SN76489_current = 0; // Currently selected register.
static BYTE8 ioMemory[4*0x4000];
static void HWWriteSoundChip(int data);
// *******************************************************************************************************************************
// Read from I/O Space
// *******************************************************************************************************************************
BYTE8 IOReadMemory(BYTE8 page,WORD16 address) {
if (page == 0) {
if (address >= 0xD640 && address < 0xD650) {
return HWReadKeyboardHardware(address);
}
if (address == 0xDC00) {
return GFXReadJoystick0();
}
}
return ioMemory[(page << 14)|(address & 0x3FFF)];
}
// *******************************************************************************************************************************
// Write to I/O Space
// *******************************************************************************************************************************
void IOWriteMemory(BYTE8 page,WORD16 address,BYTE8 data) {
if (page == 0) {
if (address == 0xD600 || address == 0xD610) {
HWWriteSoundChip(data);
}
if (address >= 0xD640 && address < 0xD650) {
HWWriteKeyboardHardware(address,data);
}
}
ioMemory[(page << 14)|(address & 0x3FFF)] = data;
}
// *******************************************************************************************************************************
// Insert/Delete Queue
// *******************************************************************************************************************************
static void HWQueueInsert(struct _Queue *q,int value) {
if (q->count < QSIZE) q->queue[q->count++] = value;
}
static int HWQueueRemove(struct _Queue *q) {
if (q->count == 0) return -1;
int r = q->queue[0];
for (int i = 0;i < q->count;i++) q->queue[i] = q->queue[i+1];
q->count--;
return r;
}
// *******************************************************************************************************************************
// Reset Hardware
// *******************************************************************************************************************************
#include "roms/foenix_charset.h"
void HWReset(void) {
keyboardQueue.count = 0;
HWResetKeyboardHardware();
for (int i = 0;i < 4;i++) {
SN76489_reg[i*2+1] = 0xF; // Set all attenuation to $F e.g. off
SN76489_reg[i*2+0] = 0; // Pitch zero.
GFXSetFrequency(i,0); // All beepers off
}
for (int i = 0;i < 0x800;i++) {
IOWriteMemory(1,0xC000+i,foenix_charset[i]);
}
}
// *******************************************************************************************************************************
// Reset CPU
// *******************************************************************************************************************************
void HWSync(void) {
if (keyboardQueue.count != 0) {
int key = HWQueueRemove(&keyboardQueue);
HWKeyboardHardwareDequeue(key);
//printf("Dequeue %x\n",key);
if (HWCheckKeyboardInterruptEnabled()) {
//printf("Interrupt\n");
CPUInterruptMaskable(); // fire IRQ
}
}
if (IOReadMemory(0,0xD658) & 1) { // Timer on.
int a = 0xD659;
int c = 1;
while (c != 0) {
int b = IOReadMemory(0,a)+1;
IOWriteMemory(0,a,b & 0xFF);
c = (b == 0x100) ? 1 : 0;
a++;
}
}
}
// *******************************************************************************************************************************
// Write to display/colour RAM
// *******************************************************************************************************************************
void HWWriteDisplay(WORD16 address,BYTE8 data) {
}
// *******************************************************************************************************************************
// Receive faux PS/2 Keyboard event
// *******************************************************************************************************************************
void HWQueueKeyboardEvent(int ps2code) {
HWQueueInsert(&keyboardQueue,ps2code);
}
// *******************************************************************************************************************************
// Write byte to 76489, handles left & right
// *******************************************************************************************************************************
static int _HWGetFrequency(void) {
int rPair = SN76489_current & 0xFE;
if (SN76489_reg[rPair+1] != 0 || SN76489_reg[rPair] == 0) return 0;
return 111563 / SN76489_reg[rPair];
}
static void HWWriteSoundChip(int data) {
int startFreq,endFreq;
if (data & 0x80) {
SN76489_current = (data >> 4) & 7;
}
startFreq = _HWGetFrequency();
if (data & 0x80) {
SN76489_reg[SN76489_current] &= 0xFFF0;
SN76489_reg[SN76489_current] |= data & 0x0F;
} else {
SN76489_reg[SN76489_current] &= 0x000F;
SN76489_reg[SN76489_current] |= ((data & 0x3F) << 4);
}
//printf("Register %d is %x %d\n",SN76489_current,SN76489_reg[SN76489_current],SN76489_reg[SN76489_current]);
endFreq = _HWGetFrequency();
if (startFreq != endFreq) {
int gChannel = (SN76489_current >> 1) ^ 3;
//printf("Changing pitch of %d to %d\n",gChannel,endFreq);
GFXSetFrequency(endFreq,gChannel);
}
//printf("Change: %d %d\n",endFreq,startFreq);
}