1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2004 Forgotten and the VBA development team
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 u8 gbPrinterStatus
= 0;
24 int gbPrinterState
= 0;
25 u8 gbPrinterData
[0x280*9];
26 u8 gbPrinterPacket
[0x400];
27 int gbPrinterCount
= 0;
28 int gbPrinterDataCount
= 0;
29 int gbPrinterDataSize
= 0;
30 int gbPrinterResult
= 0;
32 bool gbPrinterCheckCRC()
36 for(int i
= 2; i
< (6+gbPrinterDataSize
); i
++) {
37 crc
+= gbPrinterPacket
[i
];
40 int msgCrc
= gbPrinterPacket
[6+gbPrinterDataSize
] +
41 (gbPrinterPacket
[7+gbPrinterDataSize
]<<8);
49 gbPrinterDataSize
= 0;
50 gbPrinterDataCount
= 0;
56 void gbPrinterShowData()
58 systemGbPrint(gbPrinterData
,
66 set_gfx_mode(GFX_AUTODETECT, 160, 144, 0, 0);
82 u8 *data = gbPrinterData;
83 for(int y = 0; y < 0x12; y++) {
84 for(int x = 0; x < 0x14; x++) {
85 for(int k = 0; k < 8; k++) {
88 for(int j = 0; j < 8; j++) {
89 int mask = 1 << (7-j);
95 putpixel(screen, x*8+j, y*8+k, c);
101 while(!keypressed()) {
106 void gbPrinterReceiveData()
108 if(gbPrinterPacket
[3]) { // compressed
109 u8
*data
= &gbPrinterPacket
[6];
110 u8
*dest
= &gbPrinterData
[gbPrinterDataCount
];
112 while(len
< gbPrinterDataSize
) {
113 u8 control
= *data
++;
114 if(control
& 0x80) { // repeated data
117 memset(dest
, *data
++, control
);
122 memcpy(dest
, data
, control
);
129 memcpy(&gbPrinterData
[gbPrinterDataCount
],
132 gbPrinterDataCount
+= gbPrinterDataSize
;
136 void gbPrinterCommand()
138 switch(gbPrinterPacket
[2]) {
140 // reset/initialize packet
141 gbPrinterDataCount
= 0;
150 gbPrinterReceiveData();
158 u8
gbPrinterSend(u8 b
)
160 switch(gbPrinterState
) {
163 // receiving preamble
165 gbPrinterPacket
[gbPrinterCount
++] = b
;
168 // todo: handle failure
173 // receiving preamble
175 gbPrinterPacket
[gbPrinterCount
++] = b
;
178 // todo: handle failure
184 gbPrinterPacket
[gbPrinterCount
++] = b
;
185 if(gbPrinterCount
== 6) {
187 gbPrinterDataSize
= gbPrinterPacket
[4] + (gbPrinterPacket
[5]<<8);
192 if(gbPrinterDataSize
) {
193 gbPrinterPacket
[gbPrinterCount
++] = b
;
194 if(gbPrinterCount
== (6+gbPrinterDataSize
)) {
200 // intentionally move to next if no data to receive
203 gbPrinterPacket
[gbPrinterCount
++] = b
;
208 gbPrinterPacket
[gbPrinterCount
++] = b
;
209 if(gbPrinterCheckCRC()) {
216 gbPrinterPacket
[gbPrinterCount
++] = b
;
217 gbPrinterResult
= 0x81;
222 gbPrinterPacket
[gbPrinterCount
++] = b
;
223 gbPrinterResult
= gbPrinterStatus
;
228 return gbPrinterResult
;