-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApple1Terminal.cpp
193 lines (158 loc) · 3.79 KB
/
Apple1Terminal.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
#include "Apple1Terminal.h"
/*
This implementation of the terminal is not (yet) signal or shift register compliant.
Reference material:
https://www.sbprojects.net/projects/apple1/terminal.php
https://www.sbprojects.net/projects/apple1/a-one-terminal.php
*/
Apple1Terminal::Apple1Terminal(std::shared_ptr<MC6821> pia)
{
// load character ROMs
LoadCharacterRom("Apple1_charmap.rom", cCharacterRom, false);
LoadCharacterRom("Apple1_charmap.rom", cCharacterRomInverted, true);
// wire up with PIA
pia->setOutputBHandler([&](uint8_t dsp) {
ReceiveOutput(dsp);
});
}
Apple1Terminal::~Apple1Terminal()
{
}
void Apple1Terminal::ClearScreen()
{
// Clear Screen
for (int y = 0; y <= sprScreen.height; y++)
for (int x = 0; x <= sprScreen.width; x++)
sprScreen.SetPixel(x, y, olc::BLACK);
for (auto& c : cScreenBuffer)
c = ' ';
nCursorY = nCursorX = 0;
}
bool Apple1Terminal::ProcessOutput()
{
if (displayQueue.empty())
return false;
uint8_t dsp = displayQueue.front();
// make lower case key upper
if (dsp >= 0x61 && dsp <= 0x7A)
dsp &= 0x5F;
// clear old cursor
RenderCharacter(nCursorX, nCursorY, cCharacterRom[cScreenBuffer[nCursorY * nCols + nCursorX]]);
// display new character
switch (dsp)
{
case 0x0D:
nCursorX = 0;
nCursorY++;
break;
default:
if (dsp >= 0x20 && dsp <= 0x5F)
{
cScreenBuffer[nCursorY * nCols + nCursorX] = dsp;
RenderCharacter(nCursorX, nCursorY, cCharacterRom[dsp]);
nCursorX++;
}
break;
}
// check cursor position
if (nCursorX == nCols)
{
nCursorX = 0;
nCursorY++;
}
if (nCursorY == nRows)
{
// scroll up
for (int y = 0; y < nRows - 1; y++)
for (int x = 0; x < nCols; x++)
{
cScreenBuffer[y * nCols + x] = cScreenBuffer[(y + 1) * nCols + x];
RenderCharacter(x, y, cCharacterRom[cScreenBuffer[y * nCols + x]]);
}
int y = (nRows - 1);
for (int x = 0; x < nCols; x++)
{
cScreenBuffer[y * nCols + x] = ' ';
RenderCharacter(x, y, cCharacterRom[cScreenBuffer[y * nCols + x]]);
}
nCursorY--;
}
// draw new cursor
RenderCharacter(nCursorX, nCursorY, cCharacterRomInverted[cScreenBuffer[nCursorY * nCols + nCursorX]]);
displayQueue.pop();
return true;
}
olc::Sprite* Apple1Terminal::getScreenSprite()
{
return &sprScreen;
}
uint16_t Apple1Terminal::Width()
{
return nCols * nCharWidth;
}
uint16_t Apple1Terminal::Height()
{
return nRows * nCharHeight;
}
void Apple1Terminal::ReceiveOutput(uint8_t dsp)
{
displayQueue.push(dsp);
}
void Apple1Terminal::LoadCharacterRom(const std::string& sFileName, uint8_t(&rom)[256][8], bool bInvert)
{
std::ifstream ifs;
std::vector<uint8_t> vMemory;
ifs.open(sFileName, std::ifstream::binary);
if (ifs.is_open())
{
vMemory.resize(std::filesystem::file_size(sFileName));
ifs.read((char*)vMemory.data(), vMemory.size());
ifs.close();
}
// feed into character map
// flip/reverse bits from right-to-left to left-to-right
uint8_t nCharIndex = 0;
uint8_t nLineIndex = 0;
for (int c = 0; c < vMemory.size(); c++)
{
uint8_t fromMask = 0x80;
uint8_t toMask = 0x01;
uint8_t bNew = 0;
for (int bit = 0; bit < 8; bit++)
{
if ((vMemory[c] & fromMask) == fromMask) bNew |= toMask;
fromMask >>= 1;
toMask <<= 1;
}
if (bInvert)
rom[nCharIndex][nLineIndex] = ~bNew;
else
rom[nCharIndex][nLineIndex] = bNew;
nLineIndex++;
if (nLineIndex == 8)
{
nLineIndex = 0;
nCharIndex++;
}
}
}
void Apple1Terminal::RenderCharacter(uint8_t x, uint8_t y, uint8_t c[])
{
int32_t scanline = y * nCharHeight;
int32_t linepos = x * nCharWidth;
for (int r = 0; r < nCharHeight; r++)
{
uint8_t mask = c[r];
for (int c = nCharWidth; c >= 0; c--, mask >>= 1)
{
if ((mask & 1) == 1)
{
sprScreen.SetPixel(linepos + c, scanline + r, olc::DARK_GREEN);
}
else
{
sprScreen.SetPixel(linepos + c, scanline + r, olc::BLACK);
}
}
}
}