-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintCmd.c
194 lines (171 loc) · 4.57 KB
/
PrintCmd.c
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
// Original library by DragonEagles from http://shen.mansell.tripod.com/games/gameboy/gameboy.html
// Ported to GBDK 2020 by T0biasCZe
// Print Screen function added by Christianr
// Print Screen (360 tile mode) added by T0biasCZe
#include <gb/gb.h>
#include <gb/drawing.h>
#include <stdio.h>
uint8_t PrinterStatus[3];
const uint8_t PRINTER_INIT[]={
10,0x88,0x33,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00
};
const uint8_t PRINTER_STATUS[]={
10,0x88,0x33,0x0F,0x00,0x00,0x00,0x0F,0x00,0x00,0x00
};
const uint8_t PRINTER_EOF[]={
10,0x88,0x33,0x04,0x00,0x00,0x00,0x04,0x00,0x00,0x00
};
const uint8_t PRINTER_START[]={
14,0x88,0x33,0x02,0x00,0x04,0x00,0x01,0x03,0xE4,0x7F,0x6D,0x01,0x00,0x00
};
const uint8_t PRINT_TILE[]={
6,0x88,0x33,0x04,0x00,0x80,0x02
};
const uint8_t PRINTER_LINE[]={
14,0x88,0x33,0x02,0x00,0x04,0x00,0x01,0x00,0xE4,0x7F,0x6A,0x01,0x00,0x00
};
uint8_t tile_num, packet_num;
uint16_t CRC;
uint8_t SendPrinterByte(uint8_t byte){
uint8_t result;
disable_interrupts();
SB_REG = byte; //data to send
SC_REG = 0x81; //1000 0001 - start, internal clock
while (SC_REG & 0x80){} //wait until b1 reset
result = SB_REG; //return response stored in SB_REG
enable_interrupts();
return result;
}
void SendByte(uint8_t byte){
uint8_t result;
result = SendPrinterByte(byte);
PrinterStatus[0]=PrinterStatus[1];
PrinterStatus[1]=PrinterStatus[2];
PrinterStatus[2]=result;
}
void SendPrinterCommand(uint8_t *Command){
uint8_t length,index;
index=0;
length=*Command;
while(index < length){
index++;
SendByte(*(Command+index));
}
}
void PrinterInit (void)
{
tile_num = 0;
CRC = 0;
packet_num = 0;
SendPrinterCommand(PRINTER_INIT);
}
int CheckLinkCable(){
if(PrinterStatus[0] != 0){
return 2;
}
if((PrinterStatus[1] & 0xF0) != 0x80){
return 2;
}
return 0;
}
int GetPrinterStatus(){
SendPrinterCommand(PRINTER_STATUS);
return CheckLinkCable();
}
int CheckForErrors(){
if(PrinterStatus[2] & 128){
return 1;
}
if(PrinterStatus[2] & 64){
return 4;
}
if(PrinterStatus[2] & 32){
return 3;
}
return 0;
}
uint8_t CheckBusy() {
SendPrinterCommand(PRINTER_STATUS);
return (PrinterStatus[2] & 0x2);
}
uint8_t GetHigh(uint16_t w) {
return (w & 0xFF00u) >> 8;
}
uint8_t GetLow(uint16_t w) {
return (w & 0xFFu);
}
void PrintTileData(uint8_t *TileData, uint8_t lf, uint8_t num_packets){
uint8_t TileIndex;
if (tile_num == 0)
{
SendPrinterCommand(PRINT_TILE);
CRC = 0x04 + 0x80 + 0x02;
}
tile_num ++;
for(TileIndex = 0; TileIndex < 16; TileIndex++)
{
CRC += TileData[TileIndex];
SendByte(TileData[TileIndex]);
}
if (tile_num == 40)
{
SendByte(GetLow(CRC));
SendByte(GetHigh(CRC));
SendByte(0x00);
SendByte(0x00);
tile_num = 0;
CRC = 0;
packet_num ++;
if (packet_num == num_packets) // all done the page
{
SendPrinterCommand(PRINTER_EOF); // data end packet
if (lf)
SendPrinterCommand(PRINTER_START);
else
SendPrinterCommand(PRINTER_LINE);
packet_num = 0;
SendPrinterCommand(PRINTER_STATUS);
}
}
}
uint8_t fullPrinterInit(uint8_t timeout){
PrinterInit();
for(uint16_t attempts = 0; attempts < timeout*59;attempts++){
if(GetPrinterStatus() || CheckLinkCable()) wait_vbl_done();
else return 1;
}
return 0;
}
void PrintScreen(uint8_t linefeed) {
uint8_t x, y;
uint8_t p_data[16];
for (y=0; y<18; y++) {
for (x=0; x<20; x++) {
get_bkg_data(get_vram_byte(get_bkg_xy_addr(x, y)), 1, p_data);
PrintTileData(p_data, linefeed, 9);
}
}
}
/*void PrintScreen(uint8_t linefeed, uint8_t timeout) {
if(fullPrinterInit(timeout)){
uint8_t x, y;
uint8_t p_data[16];
for (y=0; y<18; y++) {
for (x=0; x<20; x++) {
get_bkg_data(get_vram_byte(get_bkg_xy_addr(x, y)), 1, p_data);
PrintTileData(p_data, linefeed, 9);
}
}
return 1;
}
else return 0;
}*/
void PrintScreen360(uint8_t linefeed){
uint8_t p_data[16];
uint8_t* curr_tile = (uint8_t*)0x8100;
for(uint16_t tile = 0; tile < 360; tile++){
vmemcpy(p_data,curr_tile,16);
PrintTileData(p_data, linefeed, 9);
curr_tile+=16;
}
}