diff src/gb/gbPrinter.cpp @ 17:75e5bb1e0aa1

going to now integrate the gb src tree since it has no dependencies
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:44:47 -0600
parents f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gb/gbPrinter.cpp	Sat Mar 03 11:44:47 2012 -0600
     1.3 @@ -0,0 +1,234 @@
     1.4 +#include <cstdio>
     1.5 +#include <cstring>
     1.6 +
     1.7 +#include "../common/System.h"
     1.8 +#include "gbPrinter.h"
     1.9 +
    1.10 +u8  gbPrinterStatus = 0;
    1.11 +int gbPrinterState  = 0;
    1.12 +u8  gbPrinterData[0x280*9];
    1.13 +u8  gbPrinterPacket[0x400];
    1.14 +int gbPrinterCount     = 0;
    1.15 +int gbPrinterDataCount = 0;
    1.16 +int gbPrinterDataSize  = 0;
    1.17 +int gbPrinterResult    = 0;
    1.18 +
    1.19 +bool gbPrinterCheckCRC()
    1.20 +{
    1.21 +	u16 crc = 0;
    1.22 +
    1.23 +	for (int i = 2; i < (6+gbPrinterDataSize); i++)
    1.24 +	{
    1.25 +		crc += gbPrinterPacket[i];
    1.26 +	}
    1.27 +
    1.28 +	int msgCrc = gbPrinterPacket[6+gbPrinterDataSize] +
    1.29 +	             (gbPrinterPacket[7+gbPrinterDataSize]<<8);
    1.30 +
    1.31 +	return msgCrc == crc;
    1.32 +}
    1.33 +
    1.34 +void gbPrinterReset()
    1.35 +{
    1.36 +	gbPrinterState     = 0;
    1.37 +	gbPrinterDataSize  = 0;
    1.38 +	gbPrinterDataCount = 0;
    1.39 +	gbPrinterCount     = 0;
    1.40 +	gbPrinterStatus    = 0;
    1.41 +	gbPrinterResult    = 0;
    1.42 +}
    1.43 +
    1.44 +void gbPrinterShowData()
    1.45 +{
    1.46 +	systemGbPrint(gbPrinterData,
    1.47 +	              gbPrinterPacket[6],
    1.48 +	              gbPrinterPacket[7],
    1.49 +	              gbPrinterPacket[8],
    1.50 +	              gbPrinterPacket[9]);
    1.51 +	/*
    1.52 +	   allegro_init();
    1.53 +	   install_keyboard();
    1.54 +	   set_gfx_mode(GFX_AUTODETECT, 160, 144, 0, 0);
    1.55 +	   PALETTE pal;
    1.56 +	   pal[0].r = 255;
    1.57 +	   pal[0].g = 255;
    1.58 +	   pal[0].b = 255;
    1.59 +	   pal[1].r = 168;
    1.60 +	   pal[1].g = 168;
    1.61 +	   pal[1].b = 168;
    1.62 +	   pal[2].r = 96;
    1.63 +	   pal[2].g = 96;
    1.64 +	   pal[2].b = 96;
    1.65 +	   pal[3].r = 0;
    1.66 +	   pal[3].g = 0;
    1.67 +	   pal[3].b = 0;
    1.68 +	   set_palette(pal);
    1.69 +	   acquire_screen();
    1.70 +	   u8 *data = gbPrinterData;
    1.71 +	   for(int y = 0; y < 0x12; y++) {
    1.72 +	   for(int x = 0; x < 0x14; x++) {
    1.73 +	    for(int k = 0; k < 8; k++) {
    1.74 +	      int a = *data++;
    1.75 +	      int b = *data++;
    1.76 +	      for(int j = 0; j < 8; j++) {
    1.77 +	        int mask = 1 << (7-j);
    1.78 +	        int c = 0;
    1.79 +	        if(a & mask)
    1.80 +	          c++;
    1.81 +	        if(b & mask)
    1.82 +	          c+=2;
    1.83 +	        putpixel(screen, x*8+j, y*8+k, c);
    1.84 +	      }
    1.85 +	    }
    1.86 +	   }
    1.87 +	   }
    1.88 +	   release_screen();
    1.89 +	   while(!keypressed()) {
    1.90 +	   }
    1.91 +	 */
    1.92 +}
    1.93 +
    1.94 +void gbPrinterReceiveData()
    1.95 +{
    1.96 +	if (gbPrinterPacket[3]) // compressed
    1.97 +	{
    1.98 +		u8 *data = &gbPrinterPacket[6];
    1.99 +		u8 *dest = &gbPrinterData[gbPrinterDataCount];
   1.100 +		int len  = 0;
   1.101 +		while (len < gbPrinterDataSize)
   1.102 +		{
   1.103 +			u8 control = *data++;
   1.104 +			if (control & 0x80) // repeated data
   1.105 +			{
   1.106 +				control &= 0x7f;
   1.107 +				control += 2;
   1.108 +				memset(dest, *data++, control);
   1.109 +				len  += control;
   1.110 +				dest += control;
   1.111 +			}
   1.112 +			else // raw data
   1.113 +			{
   1.114 +				control++;
   1.115 +				memcpy(dest, data, control);
   1.116 +				dest += control;
   1.117 +				data += control;
   1.118 +				len  += control;
   1.119 +			}
   1.120 +		}
   1.121 +	}
   1.122 +	else
   1.123 +	{
   1.124 +		memcpy(&gbPrinterData[gbPrinterDataCount],
   1.125 +		       &gbPrinterPacket[6],
   1.126 +		       gbPrinterDataSize);
   1.127 +		gbPrinterDataCount += gbPrinterDataSize;
   1.128 +	}
   1.129 +}
   1.130 +
   1.131 +void gbPrinterCommand()
   1.132 +{
   1.133 +	switch (gbPrinterPacket[2])
   1.134 +	{
   1.135 +	case 0x01:
   1.136 +		// reset/initialize packet
   1.137 +		gbPrinterDataCount = 0;
   1.138 +		gbPrinterStatus    = 0;
   1.139 +		break;
   1.140 +	case 0x02:
   1.141 +		// print packet
   1.142 +		gbPrinterShowData();
   1.143 +		break;
   1.144 +	case 0x04:
   1.145 +		// data packet
   1.146 +		gbPrinterReceiveData();
   1.147 +		break;
   1.148 +	case 0x0f:
   1.149 +		// NUL packet
   1.150 +		break;
   1.151 +	}
   1.152 +}
   1.153 +
   1.154 +u8 gbPrinterSend(u8 byte)
   1.155 +{
   1.156 +	switch (gbPrinterState)
   1.157 +	{
   1.158 +	case 0:
   1.159 +		gbPrinterCount = 0;
   1.160 +		// receiving preamble
   1.161 +		if (byte == 0x88)
   1.162 +		{
   1.163 +			gbPrinterPacket[gbPrinterCount++] = byte;
   1.164 +			gbPrinterState++;
   1.165 +		}
   1.166 +		else
   1.167 +		{
   1.168 +			// todo: handle failure
   1.169 +			gbPrinterReset();
   1.170 +		}
   1.171 +		break;
   1.172 +	case 1:
   1.173 +		// receiving preamble
   1.174 +		if (byte == 0x33)
   1.175 +		{
   1.176 +			gbPrinterPacket[gbPrinterCount++] = byte;
   1.177 +			gbPrinterState++;
   1.178 +		}
   1.179 +		else
   1.180 +		{
   1.181 +			// todo: handle failure
   1.182 +			gbPrinterReset();
   1.183 +		}
   1.184 +		break;
   1.185 +	case 2:
   1.186 +		// receiving header
   1.187 +		gbPrinterPacket[gbPrinterCount++] = byte;
   1.188 +		if (gbPrinterCount == 6)
   1.189 +		{
   1.190 +			gbPrinterState++;
   1.191 +			gbPrinterDataSize = gbPrinterPacket[4] + (gbPrinterPacket[5]<<8);
   1.192 +		}
   1.193 +		break;
   1.194 +	case 3:
   1.195 +		// receiving data
   1.196 +		if (gbPrinterDataSize)
   1.197 +		{
   1.198 +			gbPrinterPacket[gbPrinterCount++] = byte;
   1.199 +			if (gbPrinterCount == (6+gbPrinterDataSize))
   1.200 +			{
   1.201 +				gbPrinterState++;
   1.202 +			}
   1.203 +			break;
   1.204 +		}
   1.205 +		gbPrinterState++;
   1.206 +	// intentionally move to next if no data to receive
   1.207 +	case 4:
   1.208 +		// receiving CRC
   1.209 +		gbPrinterPacket[gbPrinterCount++] = byte;
   1.210 +		gbPrinterState++;
   1.211 +		break;
   1.212 +	case 5:
   1.213 +		// receiving CRC-2
   1.214 +		gbPrinterPacket[gbPrinterCount++] = byte;
   1.215 +		if (gbPrinterCheckCRC())
   1.216 +		{
   1.217 +			gbPrinterCommand();
   1.218 +		}
   1.219 +		gbPrinterState++;
   1.220 +		break;
   1.221 +	case 6:
   1.222 +		// receiving dummy 1
   1.223 +		gbPrinterPacket[gbPrinterCount++] = byte;
   1.224 +		gbPrinterResult = 0x81;
   1.225 +		gbPrinterState++;
   1.226 +		break;
   1.227 +	case 7:
   1.228 +		// receiving dummy 2
   1.229 +		gbPrinterPacket[gbPrinterCount++] = byte;
   1.230 +		gbPrinterResult = gbPrinterStatus;
   1.231 +		gbPrinterState  = 0;
   1.232 +		gbPrinterCount  = 0;
   1.233 +		break;
   1.234 +	}
   1.235 +	return gbPrinterResult;
   1.236 +}
   1.237 +