@@ -58,6 +58,8 @@ public LCD() {
58
58
this .setAlwaysOnTop (true );
59
59
this .setVisible (false );
60
60
this .setDefaultCloseOperation (HIDE_ON_CLOSE );
61
+
62
+ updateMode ();
61
63
}
62
64
63
65
public static void main (String [] args ) {
@@ -66,17 +68,30 @@ public static void main(String[] args) {
66
68
67
69
@ SuppressWarnings ("resource" )
68
70
Scanner scan = new Scanner (System .in );
69
- System .out .println ("[RS] [data]" );
71
+ System .out .println ("[R/W] [ RS] [data]" );
70
72
71
73
while (true ) {
72
74
String input = scan .nextLine ();
73
75
74
- boolean rs = input .charAt (0 ) == '1' ? true : false ;
75
- byte data = Byte .parseByte (input .substring (2 ,10 ), 2 );
76
-
77
- System .out .println (rs ? "Data" : "Instruction" + ": 0x" + ROMLoader .byteToHexString (data ));
76
+ char rw = input .charAt (0 );
77
+ boolean rs = input .charAt (2 ) == '1' ? true : false ;
78
78
79
- lcd .write (rs , data );
79
+ if (Character .toUpperCase (rw ) == 'W' ) {
80
+ //Write
81
+
82
+ try {
83
+ byte data = (byte )Integer .parseInt (input .substring (4 ,12 ), 2 );
84
+ System .out .println ((rs ? "Data" : "Instruction" ) + ": 0x" + ROMLoader .byteToHexString (data ));
85
+ lcd .write (rs , data );
86
+ } catch (Exception e ) {
87
+ System .out .println ("Error!" );
88
+ }
89
+
90
+ } else {
91
+ //Read
92
+ byte readData = lcd .read (rs );
93
+ System .out .println ("Read byte 0x" + ROMLoader .byteToHexString (readData ));
94
+ }
80
95
}
81
96
}
82
97
@@ -105,7 +120,21 @@ public void reset() {
105
120
public void write (boolean regSel , byte data ) {
106
121
if (regSel == false ) {
107
122
//INSTRUCTION
108
- if ((data & 0b00100000) == 0b00100000) {
123
+ if ((data & 0b10000000) == 0b10000000) {
124
+ //Set DDRAM Address
125
+ int newPos = (data & 0b01111111);
126
+
127
+ if (newPos >= 0 && newPos < text .length ) {
128
+ cursorPos = (data & 0b01111111);
129
+ } else {
130
+ cursorPos = 0 ;
131
+ }
132
+
133
+ } else if ((data & 0b01000000) == 0b01000000) {
134
+ //Set CGRAM Address
135
+ System .out .println ("LCD: Tried to set CGRAM Address, that is unimplemented!" );
136
+
137
+ } else if ((data & 0b00100000) == 0b00100000) {
109
138
//FUNCTION SET
110
139
if ((data & 0b00010000) == 0b00010000) {
111
140
fourBitMode = false ;
@@ -184,13 +213,15 @@ public void write(boolean regSel, byte data) {
184
213
}
185
214
if (debug )
186
215
System .out .println ("Cleared!" );
216
+ } else {
217
+ System .out .println ("Tried to do invalid instruction " +ROMLoader .byteToHexString (data ));
187
218
}
188
219
} else {
189
220
//DATA
190
221
text [cursorPos ] = (char )data ;
191
222
int prevCursorPos = cursorPos ;
192
223
cursorPos += increment ? 1 : -1 ;
193
- if (cursorPos = = text .length ) {
224
+ if (cursorPos > = text .length ) {
194
225
cursorPos = 0 ;
195
226
} else if (cursorPos < 0 ) {
196
227
cursorPos = 0 ;
@@ -202,7 +233,20 @@ public void write(boolean regSel, byte data) {
202
233
203
234
//Read from LCD
204
235
public byte read (boolean regSel ) {
205
- return 0x0 ;
236
+ if (debug )
237
+ System .out .println ("Reading from LCD with regSel" +(regSel ? '1' :'0' ));
238
+
239
+ byte retVal = 0 ;
240
+
241
+ if (regSel ) {
242
+ //Read from RAM
243
+ retVal = (byte )(text [cursorPos ]);
244
+ } else {
245
+ //Read busy flag and address
246
+ retVal = (byte )(127 & cursorPos );
247
+ }
248
+
249
+ return retVal ;
206
250
}
207
251
208
252
public class LCDPanel extends JPanel {
0 commit comments