Repository navigation
Expand file tree
/
Copy pathcga.py
More file actions
287 lines (243 loc) · 10.6 KB
/
Copy pathcga.py
File metadata and controls
287 lines (243 loc) · 10.6 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
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
from enum import Enum
from typing import override, List, Tuple
import font
import graphics
import m6845
import mda
import time
class CGA(mda.MDA):
class CGAMode(Enum):
Text40 = 0
Text80 = 1
G320 = 2
G640 = 3
def __init__(self, palette_per_scanline: bool):
super().__init__()
self._ram_offset = 0xb8000
self._m6845 = m6845.M6845()
self._m6845_reg = 0
self._graphics_mode = 255
self._cursor_location = -1
self._cga_mode = self.CGAMode.Text80
self._color_configuration = 0
self._color_configuration_changed = False
self._color_update_line_count = 0
self._render_version = 1
self._pulse_vsync = False
self._palette_index: List = [ 0x00 ] * 200
self._palette_per_scanline = palette_per_scanline
@override
def GetName(self) -> str:
return "CGA"
def GetTextColumns(self):
return 40 if self._cga_mode == self.CGAMode.Text40 else 80
def GetTextAddressMask(self):
return len(self._ram) - 1
def ReadTextByte(self, offset):
return self._ram[offset & self.GetTextAddressMask()]
def DecodeTextAttribute(self, attributes):
return (attributes & 0x0f, (attributes >> 4) & 0x07,
bool(attributes & 0x80))
@override
def RegisterDevice(self, mappings: dict):
mappings[0x3d0] = self
mappings[0x3d1] = self
mappings[0x3d2] = self
mappings[0x3d3] = self
mappings[0x3d4] = self
mappings[0x3d5] = self
mappings[0x3d6] = self
mappings[0x3d7] = self
mappings[0x3d8] = self
mappings[0x3d9] = self
mappings[0x3da] = self
mappings[0x3db] = self
mappings[0x3dc] = self
def GetCurrentScanLine(self):
# 304 cpu cycles per scan line
# 262 scan lines
return (self._clock // 304) % 262
def IsInHSync(self):
pixel = self._clock % 304
return pixel < 16 or pixel > 280 # TODO
def IsInVSync(self):
scan_line = self.GetCurrentScanLine()
return scan_line < 16 or scan_line >= 216
@override
def IO_Read(self, port: int) -> int:
rc = 0
if (port == 0x3d5 or port == 0x3d7) and self._m6845_reg >= 0x0c:
rc = self._m6845.Read(self._m6845_reg)
elif port == 0x3da:
if self.IsInVSync():
rc = 9 # regen buffer | 8 in vertical retrace
elif self.IsInHSync():
rc = 1
else:
rc = 0
elif port == 0x3d8:
rc = self._graphics_mode
return rc
@override
def IO_Write(self, port: int, value: int) -> bool:
if port == 0x3d4 or port == 0x3d6 or port == 0x3d0 or port == 0x3d2:
self._m6845_reg = value
elif port == 0x3d5 or port == 0x3d7 or port == 0x3d1 or port == 0x3d3:
self._m6845.Write(self._m6845_reg, value)
if self._m6845_reg == 12 or self._m6845_reg == 13:
word_address = (self._m6845.Read(12) << 8) | self._m6845.Read(13)
self._display_address = (word_address << 1) & self.GetTextAddressMask()
if self._m6845_reg == 14 or self._m6845_reg == 15:
self._cursor_location = (self._m6845.Read(14) << 8) | self._m6845.Read(15)
elif port == 0x3d8:
if self._graphics_mode != value:
prev_cga_mode = self._cga_mode
if (value & 2) == 2: # graphics 320x200
if (value & 16) == 16: # graphics 640x200
self._cga_mode = self.CGAMode.G640
else:
self._cga_mode = self.CGAMode.G320
else:
if (value & 1) == 1:
self._cga_mode = self.CGAMode.Text80
else:
self._cga_mode = self.CGAMode.Text40
self._graphics_mode = value
if self._cga_mode != prev_cga_mode:
self._cursor_location = -1
elif port == 0x3d9:
self._color_configuration = (value >> 4) & 3
self._color_configuration_changed = True
self._color_update_line_count = 0
self._last_update += 1
self._mark_frame_dirty()
return False
def GetPixelColor(self, line: int, color_index: int, rgb: List): # TODO
if self._palette_index[line] >= 2:
brightness = 255 if self._palette_index[line] == 3 else 200
if color_index == 1:
rgb[1] = rgb[2] = brightness # cyan
rgb[0] = 0
elif color_index == 2:
rgb[0] = rgb[2] = brightness # magenta
rgb[1] = 0
elif color_index == 3:
rgb[0] = rgb[1] = rgb[2] = brightness # white
else:
rgb[0] = rgb[1] = rgb[2] = 0 # black
else:
brightness = 255 if self._palette_index[line] == 1 else 200
if color_index == 1: # green
rgb[0] = rgb[2] = 0
rgb[1] = brightness
elif color_index == 2: # red
rgb[0] = brightness
rgb[1] = rgb[2] = 0
elif color_index == 3: # blue
rgb[0] = rgb[1] = 0
rgb[2] = brightness
else:
rgb[0] = rgb[1] = rgb[2] = 0
def RenderG320FrameGraphical(self):
try:
rgb = [ 0 ] * 3
for addr in range(self._display_address, min(self._display_address + 16000, 16384)):
if addr - self._display_address >= 8192:
addr_without_base = addr - 8192 - self._display_address
y = addr_without_base // 80 * 2 + 1
x = (addr_without_base % 80) * 4
else:
addr_without_base = addr - self._display_address
y = addr_without_base // 80 * 2
x = (addr_without_base % 80) * 4
if y >= 200:
continue
b = self._ram[addr]
y_color_index = y if self._palette_per_scanline else 0
if self._palette_index[y_color_index] >= 2:
brightness = 255 if self._palette_index[y_color_index] == 3 else 200
palette = [
(0, 0, 0),
(0, brightness, brightness),
(brightness, 0, brightness),
(brightness, brightness, brightness)
]
else:
brightness = 255 if self._palette_index[y_color_index] == 1 else 200
palette = [
(0, 0, 0),
(0, brightness, 0),
(brightness, 0, 0),
(0, 0, brightness)
]
y_offset = y * 320 * 4 * 4
for x_i in range(4):
color_index = (b >> (x_i * 2)) & 3
x_offset = (x + 3 - x_i) * 4 * 2
offset = y_offset + x_offset
p = palette[color_index]
self._pixels[offset + 0] = self._pixels[offset + 4] = p[2]
self._pixels[offset + 1] = self._pixels[offset + 5] = p[1]
self._pixels[offset + 2] = self._pixels[offset + 6] = p[0]
offset += 320 * 4 * 2
self._pixels[offset + 0] = self._pixels[offset + 4] = p[2]
self._pixels[offset + 1] = self._pixels[offset + 5] = p[1]
self._pixels[offset + 2] = self._pixels[offset + 6] = p[0]
return 640, 400, self._pixels
except Exception as e:
print(f'CGA::RenderG320FrameGraphical exception: {e}, line number: {e.__traceback__.tb_lineno}')
def RenderG640FrameGraphical(self):
try:
for addr in range(self._display_address, min(self._display_address + 16000, 16384)):
if addr - self._display_address >= 8192:
addr_without_base = addr - 8192 - self._display_address
y = addr_without_base // 80 * 2 + 1
x = (addr_without_base % 80) * 8
else:
addr_without_base = addr - self._display_address
y = addr_without_base // 80 * 2
x = (addr_without_base % 80) * 8
if y >= 200:
continue
b = self._ram[addr]
for x_i in range(8):
value = 255 if b & 1 else 0
offset1 = (y * 640 * 2 + x + 7 - x_i) * 4
self._pixels[offset1 + 0] = self._pixels[offset1 + 1] = self._pixels[offset1 + 2] = value
offset2 = offset1 + 640 * 4
self._pixels[offset2 + 0] = self._pixels[offset2 + 1] = self._pixels[offset2 + 2] = value
b >>= 1
return 640, 400, self._pixels
except Exception as e:
print(f'CGA::RenderG640FrameGraphical exception: {e}, line number: {e.__traceback__.tb_lineno}')
def GetFrame(self):
try:
if self._cga_mode == self.CGAMode.Text40 or self._cga_mode == self.CGAMode.Text80:
return self.RenderTextFrameGraphical()
if self._cga_mode == self.CGAMode.G320:
return self.RenderG320FrameGraphical()
if self._cga_mode == self.CGAMode.G640:
return self.RenderG640FrameGraphical()
except Exception as e:
print(f'CGA::GetFrame exception: {e}, line number: {e.__traceback__.tb_lineno}')
@override
def Tick(self, cycles: int, clock: int) -> bool:
self._clock = clock
line = (clock // 304) % 262
if self._color_configuration_changed:
# 200: there's also a 160x100 mode for which this needs to be adjusted
if line >= 16 and line < 216:
self._palette_index[line - 16] = self._color_configuration
if self._palette_per_scanline == False:
self._palette_index[0] = self._color_configuration
self._color_update_line_count += 1
if self._color_update_line_count >= 200:
self._color_configuration_changed = False
if line >= 216: # VSync start
if self._pulse_vsync == False:
self._pulse_vsync = True
self._last_update += 1
#self.PublishVSync()
else:
self._pulse_vsync = False
return False