-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinTrs80.cpp
302 lines (230 loc) · 6.55 KB
/
WinTrs80.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
#include "stdafx.h"
#include "MainApp.h"
#include "ChildView.h"
#include "MainFrm.h"
#include "WinTrs80.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//**************************************************
CWinTrs80::CWinTrs80()
{
// If this is changed, change GetDisplaySize
if (!m_font.CreateFont(21, 10, 0, 0, FW_NORMAL, FALSE, FALSE,
FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|TMPF_TRUETYPE,
"courier new"))
{
TRACE0("Failed to create font in CWinTrs80\n");
}
for (int i=0; i < sizeof(m_is_key_down); i++)
m_is_key_down[i] = 0;
}
//*************************************************
CWinTrs80::~CWinTrs80()
{
}
//**************************************************
void CWinTrs80::AttachCDC(CDC *dc)
{
m_dc = dc;
// select our custom font into the cdc
m_oldfont = dc->SelectObject(&m_font);
if (!m_oldfont)
{
TRACE0("Error selecting font in CWinTrs80::AttachCDC()\n");
}
// cache some stuff for speed
m_char_size = dc->GetTextExtent("X",1); // font is fixed, so any char will work
// Calc each cell seperately to avoid rounding errors
m_cell_w1 = m_char_size.cx / 2;
m_cell_w2 = m_char_size.cx - m_cell_w1;
m_cell_h1 = m_char_size.cy / 3;
m_cell_h2 = m_char_size.cy / 3;
m_cell_h3 = m_char_size.cy - m_cell_h1 - m_cell_h2;
m_color = dc->GetTextColor();
}
//**************************************************
void CWinTrs80::DetachCDC()
{
// restore original font
if (m_oldfont)
m_dc->SelectObject(m_oldfont);
m_dc = NULL;
}
//**************************************************
// static function
void CWinTrs80::GetDisplaySize(CDC *dc, CSize *size)
{
CRect rect;
CFont *oldfont;
CFont display_font;
// If this is changed, change CWinTrs80 contructor as well
if (!display_font.CreateFont(21, 10, 0, 0, FW_NORMAL, FALSE, FALSE,
FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|TMPF_TRUETYPE,
"courier new"))
{
TRACE0("Failed to create font in CWinTrs80\n");
}
oldfont = dc->SelectObject(&display_font);
ASSERT(oldfont);
*size = dc->GetTextExtent("X",1); // font is fixed, so any char will work
if (oldfont)
dc->SelectObject(oldfont);
size->cx=size->cx*64+WIN_X_OFFSET*2;
size->cy=size->cy*16+WIN_Y_OFFSET*2;
}
//**************************************************
//
// ASCII chars except for TRS80_KEY_ which are mapped in to ASCII
//
void CWinTrs80::SetCharDown(unsigned char c, unsigned char oem_code)
{
m_is_key_down[c] = 1;
m_key_oem_code[c] = oem_code;
// TRACE("key down %x %x\n", c, oem_code);
}
//**************************************************
//
// WM_KEYUP messages, so there are lots of them
//
void CWinTrs80::SetCharUp(unsigned char c, unsigned char oem_code)
{
// TRACE("key up %x %x\n", c, oem_code);
if (c == TRS80_KEY_SHIFT)
m_is_key_down[TRS80_KEY_SHIFT] = 0;
else if (c == TRS80_KEY_CTRL)
m_is_key_down[TRS80_KEY_CTRL] = 0;
else
{
for (short i=TRS80_KEY_LAST_SPECIAL+1; i < 256; i++)
if (m_is_key_down[i] && m_key_oem_code[i]==oem_code)
{
m_is_key_down[i] = 0;
break;
}
if (i >= 256)
{
TRACE("CWinTrs80::SetCharU Error: Can't find oem_code %x\n", oem_code);
}
}
}
//**************************************************
unsigned char* CWinTrs80::PeekKeyStatus()
{
return m_is_key_down;
}
//**************************************************
void CWinTrs80::Paint(CDC* dc)
{
CFont *oldfont;
oldfont = dc->SelectObject(&m_font);
if (!oldfont)
{
TRACE0("Error selecting font in CWinTrs80::Paint()\n");
}
for (int i=0; i < 1024; i++)
DrawChar(dc, i, m_memory[0x3C00+i]);
if (oldfont)
dc->SelectObject(oldfont);
}
//**************************************************
void CWinTrs80::DrawChar(unsigned short offset, unsigned char value)
{
DrawChar(m_dc, offset, value);
}
//**************************************************
void CWinTrs80::DrawChar(CDC* dc, unsigned short offset, unsigned char value)
{
if (value&128 || value==32)
{
DrawTrs80Graphic(dc, offset, value);
}
else
{
int x, y;
x = WIN_X_OFFSET + (offset%64)*m_char_size.cx;
y = WIN_X_OFFSET + (offset/64)*m_char_size.cy;
if (!m_config->m_lower_case_mod_installed)
{
if ((value & 32) == 0) // When No lowercase mod installed
value |= 64; // BIT6 = NOT (BIT5 OR BIT7)
else // this part not needed -- why?
value &= (~64); // BIT6 = NOT (BIT5 OR BIT7)
}
dc->TextOut(x, y, (char *)&value, 1);
::GdiFlush();
}
}
//**************************************************
void CWinTrs80::DrawTrs80Graphic(CDC* dc, unsigned short offset, unsigned char val)
{
int x, y;
COLORREF bg;
x = WIN_X_OFFSET + (offset%64)*m_char_size.cx;
y = WIN_X_OFFSET + (offset/64)*m_char_size.cy;
bg = dc->GetBkColor();
dc->FillSolidRect(x, y, m_char_size.cx, m_char_size.cy, bg);
if (val == 32)
return;
if (val&1)
dc->FillSolidRect(x, y, m_cell_w1, m_cell_h1, m_color);
y=y+m_cell_h1;
if (val&4)
dc->FillSolidRect(x, y, m_cell_w1, m_cell_h2, m_color);
y=y+m_cell_h2;
if (val&16)
dc->FillSolidRect(x, y, m_cell_w1, m_cell_h3, m_color);
x=x+m_cell_w1;
if (val&32)
dc->FillSolidRect(x, y, m_cell_w2, m_cell_h3, m_color);
y=y-m_cell_h2;
if (val&8)
dc->FillSolidRect(x, y, m_cell_w2, m_cell_h2, m_color);
y=y-m_cell_h1;
if (val&2)
dc->FillSolidRect(x, y, m_cell_w2, m_cell_h1, m_color);
dc->SetBkColor(bg);
}
//
// Tell the system timer to measure time from this point on ward
//
void CWinTrs80::ResetHostElapsedTime()
{
LARGE_INTEGER lpFrequency;
VERIFY(QueryPerformanceFrequency(&lpFrequency));
m_sys_freq = lpFrequency.QuadPart;
QueryPerformanceCounter(&m_sys_time_start);
}
//
// Return the number of usecs that have elapsed since the last call to ResetHostElapsedTime()
// Used to throttle the Z80 speed to the speed the TRS80 originally ran at
//
__int64 CWinTrs80::GetHostElapsedTime()
{
LARGE_INTEGER sys_time_now;
__int64 elapsed_sys_time;
VERIFY(QueryPerformanceCounter(&sys_time_now));
elapsed_sys_time = sys_time_now.QuadPart - m_sys_time_start.QuadPart;
elapsed_sys_time = (elapsed_sys_time*1000000)/m_sys_freq;
return elapsed_sys_time;
}
//
// Draw into the back buffer but once each sleep (nominally 10ms), update the display
//
void CWinTrs80::AboutToSleep()
{
extern CMainApp theApp;
CChildView *view;
HRESULT ddrval;
HDC hdc;
view = (CChildView *)&(((CMainFrame *)theApp.m_pMainWnd)->m_wndView);\
view->lpDDSBack->ReleaseDC(*m_dc);
m_dc->Detach();
ddrval = view->lpDDSPrimary->Blt(&view->m_direct_draw_into_rect, view->lpDDSBack, NULL, DDBLT_WAIT, NULL );
view->lpDDSBack->GetDC(&hdc);
m_dc->Attach(hdc);
}