forked from uli/cascade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.cpp
237 lines (223 loc) · 5.25 KB
/
lcd.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
/*
* lcd.cpp
*
* (C) Copyright 2014 Ulrich Hecht
*
* This file is part of CASCADE. CASCADE is almost free software; you can
* redistribute it and/or modify it under the terms of the Cascade Public
* License 1.0. Read the file "LICENSE" for details.
*/
#include "lcd.h"
#include "debug.h"
#include <stdlib.h>
#include <string.h>
#define LAYER2_OFFSET 0x2580
#ifndef NDEBUG
static int coords_x(uint16_t cursor)
{
if (cursor >= LAYER2_OFFSET)
cursor -= LAYER2_OFFSET;
return cursor * 8 % SRC_WIDTH;
}
static int coords_y(uint16_t cursor)
{
if (cursor >= LAYER2_OFFSET)
cursor -= LAYER2_OFFSET;
return cursor * 8 / SRC_WIDTH;
}
#endif
Lcd::Lcd(UI *ui)
{
mem = (uint8_t *)malloc(65536);
this->ui = ui;
reset();
}
Lcd::~Lcd()
{
free(mem);
}
void Lcd::reset()
{
memset(mem, 0, 65536);
state = CMD_IDLE;
next_param = 0;
cursor = 0;
dirty = true;
}
uint8_t Lcd::read(int a0)
{
uint8_t ret = 0xff;
if (a0 == LCD_READ_STATUS) {
ERROR("LCD unimplemented status read\n");
exit(1);
}
else {
switch (state) {
case CMD_MREAD:
ret = mem[cursor];
DEBUG(LCD, "LCD memread %02X ('%c') <- %04X (%d/%d)\n", ret, ret, cursor, coords_x(cursor), coords_y(cursor));
cursor++;
break;
default:
ERROR("LCD unknown state when reading\n");
exit(1);
break;
};
}
DEBUG(LCD, "LCD read %d: %02X\n", a0, ret);
return ret;
}
void Lcd::write(int a0, uint8_t val)
{
DEBUG(LCD, "LCD write %d: %02X\n", a0, val);
if (a0 == LCD_WRITE_COMMAND) {
switch (val) {
case 0x42:
state = CMD_MWRITE;
break;
case 0x43:
state = CMD_MREAD;
break;
case 0x46:
state = CMD_CSRW;
next_param = 0;
break;
case 0x58:
case 0x59:
DEBUG(LCD, "LCD display %s\n", (val & 1) ? "on" : "off");
display_on = val & 1;
state = CMD_DISPONOFF;
next_param = 0;
break;
case 0x5b:
state = CMD_OVLAY;
next_param = 0;
break;
default:
ERROR("LCD unimplemented command %02X\n", val);
exit(1);
break;
};
}
else {
switch (state) {
case CMD_MWRITE:
DEBUG(LCD, "LCD memwrite %02X ('%c') -> %04X (%d/%d)\n", val, val, cursor, coords_x(cursor), coords_y(cursor));
mem[cursor] = val;
dirty = true;
cursor++;
break;
case CMD_CSRW:
if (next_param == 0)
cursor = val;
else if (next_param == 1) {
cursor |= val << 8;
DEBUG(LCD, "LCD cursor write %04X (%d/%d)\n", cursor, coords_x(cursor), coords_y(cursor));
}
else {
ERROR("LCD error in CSRW\n");
exit(1);
}
next_param++;
break;
case CMD_OVLAY:
if (next_param == 0) {
mx = val & 3;
/* dm is actually two bits, but they are required to be
the same, so we just use one */
dm = (val >> 2) & 1;
ov = (val >> 4) & 1;
DEBUG(LCD, "LCD overlay mx %d dm %d ov %d\n", mx, dm, ov);
}
else {
ERROR("LCD error in OVLAY\n");
exit(1);
}
next_param++;
break;
case CMD_DISPONOFF:
if (next_param == 0) {
fc = val & 3;
fp1 = (val >> 2) & 3; /* attributes first block */
fp2_4 = (val >> 4) & 3; /* second, fourth block */
fp3 = (val >> 6) & 3; /* third block */
DEBUG(LCD, "LCD fc %02X fp1 %02X fp2/4 %02X fp3 %02X\n", fc, fp1, fp2_4, fp3);
}
else {
ERROR("LCD error in DISP ON/OFF\n");
exit(1);
}
break;
default:
ERROR("LCD unknown state %d when writing %02X to %d\n",
state, val, a0);
exit(1);
break;
};
}
}
void Lcd::update()
{
if (dirty) {
uint16_t *pp = (uint16_t *)ui->getPixels();
if (!pp) /* painting disabled */
return;
dirty = false;
int i, j, lc;
#ifdef SCALE_SCREEN
int line = 0;
#endif
uint8_t pixels;
int display_x = ui->screenStep();
pp += ui->screenX();
pp += ui->screenY() * display_x;
for (i = 0, lc = 0; i < SRC_WIDTH*SRC_HEIGHT; i+=8, lc+=8) {
if (lc == SRC_WIDTH) {
pp += display_x - DST_WIDTH;
#ifdef SCALE_SCREEN
if (line % DOUBLE_Y_EVERY == (DOUBLE_Y_EVERY - 1)) {
/* copy previous line */
memcpy(pp, pp - display_x, DST_WIDTH * 2);
pp += display_x;
}
line++;
#endif
lc = 0;
}
pixels = (mem[i/8]) ^ (mem[LAYER2_OFFSET + i/8]);
for (j = 0; j < 8; j++) {
*pp++ = (pixels >> 7) - 1;
#ifdef SCALE_SCREEN
if (j % DOUBLE_X_EVERY == DOUBLE_X_EVERY - 1)
*pp++ = (pixels >> 7) - 1;
#endif
pixels <<= 1;
}
}
#ifdef SCALE_SCREEN
/* copy previous line */
memcpy(pp, pp - display_x, DST_WIDTH * 2);
#endif
ui->setDirty();
}
ui->flip();
}
#include "state.h"
void Lcd::loadSaveState(statefile_t fp, bool write)
{
STATE_RWBUF(mem, 65536);
STATE_RW(state);
STATE_RW(next_param);
STATE_RW(cursor);
STATE_RW(mx);
STATE_RW(dm);
STATE_RW(ov);
STATE_RW(display_on);
STATE_RW(fc);
STATE_RW(fp1); STATE_RW(fp2_4); STATE_RW(fp3);
}
void Lcd::redraw()
{
dirty = true;
update();
}