//video gen //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //PORT D.0,D.1 = UART communication with mouse mcu #pragma regalloc- //I allocate the registers myself #pragma optsize- //optimize for speed #include #include #include #include #include //cycles = 63.625 * 16 Note NTSC is 63.55 //but this line duration makes each frame exactly 1/60 sec //which is nice for keeping a realtime clock #define lineTime 1018 #define begin { #define end } #define ScreenTop 30 #define ScreenBot 230 // Commands that the mouse mcu can send to the tv mcu. #define UART_UPDATE_CURSOR 0xf1 //NOTE that v1 to v8 and i must be in registers! register char v1 @4; register char v2 @5; register char v3 @6; register char v4 @7; register char v5 @8; register char v6 @9; register char v7 @10; register char v8 @11; register int i @12; #pragma regalloc+ char syncON, syncOFF; int LineCount; int time; //Variables to hold last and current cursor position: unsigned char last_cursor_x, last_cursor_y, cursor_x, cursor_y, button_state; //Variables used for the calculation of circle and rectangle drawing: unsigned char mid_x,mid_y,rad_x,rad_y; unsigned char last_mid_x,last_mid_y,last_rad_x,last_rad_y; //variables for receiving command from other mcu. unsigned char temp,r_index,UART_buffer[20],cmd_ready,str; #define TOOL_POINT 0 //Single pixel tool #define TOOL_LINE 1 //Line tool #define TOOL_SPRAY 2 //Spray can tool #define TOOL_ERASER 3 //Eraser tool #define TOOL_FILL 4 //Large Brush tool #define TOOL_RECT 5 //Rectangle tool #define TOOL_OVAL 6 //Oval tool #define TOOL_CLEAR 7 //clear screen (not active tool) //Stores the current tool unsigned char cur_tool; //Stores the state of the mouse button: unsigned char button_up,button_down,button_dragging; //Stores offset for index in the bitmap array to draw "random" spray can frames: unsigned char spray_frame; //Stores the location of initial mouse down for line, rectangle and circle tools: unsigned char click_origin_x,click_origin_y; //Stores the original color(black or white) of the pixel where the cursor is: unsigned char last_cursor_c; //Used to prevent user from drawing on the tool set on the left side of the screen: unsigned char x_draw_limit; //Screen array: char screen[1600], t, ts[10]; //The names of the tools to written to screen when selected: char toolname[8][6] = {"POINT","LINE@","SPRAY","ERASE","BRUSH","RECT@","OVAL@","CLEAR"}; //Bitmaps which include icons for tools, and the frames of randomness for the spray can: flash char icon_bitmap[16][7]={ //dot(free-form drawing) 0b00000000, 0b00000000, 0b00011000, 0b00011000, 0b00000000, 0b00000000, 0b00000000, //line 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b00000000, //spray_icon 0b01001000, 0b00000010, 0b10010000, 0b00000100, 0b00100000, 0b00000010, 0b00010000, //erase 0b00001111, 0b00010011, 0b00100101, 0b01001010, 0b11110100, 0b10011000, 0b11110000, //fill 0b00011000, 0b00111100, 0b01111110, 0b11111111, 0b01111110, 0b00111100, 0b00011000, //box 0b00000000, 0b11111111, 0b10000001, 0b10000001, 0b11111111, 0b00000000, 0b00000000, //circle 0b00011000, 0b00100100, 0b01000010, 0b01000010, 0b00100100, 0b00011000, 0b00000000, //X (clear screen) 0b01000100, 0b00101000, 0b00010000, 0b00101000, 0b01000100, 0b00000000, 0b00000000, //spray1 0b00010000, 0b01000100, 0b00011000, 0b01010010, 0b00110001, 0b00000110, 0b00001000, //spray2 0b00000000, 0b00010100, 0b01001001, 0b01101011, 0b01000100, 0b00010000, 0b00000100, //spray3 0b00001000, 0b00000010, 0b10010000, 0b00000100, 0b00100000, 0b00000010, 0b00010000, //blank 0b00010000, 0b00010000, 0b00000000, 0b00000100, 0b01000000, 0b00001000, 0b00000100, //spray4 0b00100000, 0b00000000, 0b00100100, 0b00000000, 0b10000010, 0b00100000, 0b00100100, //spray5 0b00000100, 0b00100000, 0b00100010, 0b10001000, 0b00001000, 0b01001000, 0b00010000, //spray6 0b00000000, 0b00001000, 0b00010000, 0b01000000, 0b00000100, 0b01000000, 0b00001000, //blank 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 }; //Sine table used for circle drawing: flash double sin_table[360]={ 0, 0.017452392, 0.034899467, 0.052335912, 0.069756415, 0.087155669, 0.104528375, 0.121869241, 0.139172984, 0.156434334, 0.173648032, 0.190808836, 0.207911518, 0.224950868, 0.241921695, 0.258818832, 0.275637129, 0.292371465, 0.309016742, 0.32556789, 0.342019866, 0.358367661, 0.374606293, 0.390730816, 0.40673632, 0.422617928, 0.438370802, 0.453990145, 0.469471198, 0.484809246, 0.499999617, 0.515037683, 0.529918864, 0.544638627, 0.559192488, 0.573576014, 0.587784823, 0.601814588, 0.615661034, 0.629319944, 0.642787158, 0.656058573, 0.669130146, 0.681997896, 0.694657904, 0.707106312, 0.719339329, 0.731353229, 0.743144352, 0.754709106, 0.766043969, 0.777145488, 0.788010282, 0.79863504, 0.809016526, 0.819151579, 0.829037111, 0.83867011, 0.848047643, 0.857166853, 0.866024962, 0.874619271, 0.882947164, 0.891006103, 0.898793633, 0.906307382, 0.913545062, 0.920504468, 0.927183479, 0.933580062, 0.939692268, 0.945518235, 0.951056188, 0.956304441, 0.961261395, 0.96592554, 0.970295455, 0.974369809, 0.978147362, 0.981626961, 0.984807548, 0.987688154, 0.990267901, 0.992546003, 0.994521766, 0.996194589, 0.997563962, 0.998629468, 0.999390782, 0.999847672, 1.0, 0.999847719, 0.999390874, 0.998629607, 0.997564147, 0.99619482, 0.994522043, 0.992546326, 0.99026827, 0.987688569, 0.984808009, 0.981627468, 0.978147913, 0.974370406, 0.970296097, 0.965926227, 0.961262127, 0.956305217, 0.951057008, 0.945519099, 0.939693175, 0.933581013, 0.927184473, 0.920505504, 0.913546141, 0.906308504, 0.898794796, 0.891007307, 0.88294841, 0.874620558, 0.866026288, 0.857168219, 0.848049049, 0.838671556, 0.829038595, 0.819153101, 0.809018086, 0.798636637, 0.788011915, 0.777147158, 0.766045675, 0.754710847, 0.743146128, 0.731355039, 0.719341173, 0.707108188, 0.694659813, 0.681999837, 0.669132118, 0.656060576, 0.642789191, 0.629322006, 0.615663125, 0.601816707, 0.58778697, 0.573578187, 0.559194688, 0.544640852, 0.529921115, 0.515039958, 0.500001915, 0.484811567, 0.469473541, 0.453992509, 0.438373187, 0.422620333, 0.406738744, 0.390733259, 0.374608753, 0.358370138, 0.34202236, 0.325570399, 0.309019266, 0.292374003, 0.27563968, 0.258821395, 0.24192427, 0.224953453, 0.207914113, 0.190811441, 0.173650646, 0.156436955, 0.139175612, 0.121871875, 0.104531014, 0.087158313, 0.069759062, 0.052338562, 0.034902119, 0.017455045, 0.0, -0.017449739, -0.034896815, -0.052333262, -0.069753768, -0.087153026, -0.104525736, -0.121866607, -0.139170356, -0.156431713, -0.173645419, -0.190806231, -0.207908922, -0.224948282, -0.241919121, -0.258816268, -0.275634578, -0.292368927, -0.309014218, -0.325565381, -0.342017373, -0.358365183, -0.374603832, -0.390728374, -0.406733896, -0.422615523, -0.438368417, -0.453987781, -0.469468855, -0.484806925, -0.499997319, -0.515035409, -0.529916614, -0.544636402, -0.559190288, -0.57357384, -0.587782676, -0.601812468, -0.615658943, -0.629317882, -0.642785125, -0.65605657, -0.669128174, -0.681995956, -0.694655995, -0.707104436, -0.719337486, -0.731351419, -0.743142576, -0.754707365, -0.766042264, -0.777143818, -0.788008648, -0.798633443, -0.809014967, -0.819150057, -0.829035627, -0.838668665, -0.848046237, -0.857165486, -0.866023635, -0.874617985, -0.882945918, -0.891004898, -0.898792469, -0.906306261, -0.913543983, -0.920503431, -0.927182485, -0.933579111, -0.93969136, -0.945517371, -0.951055368, -0.956303665, -0.961260664, -0.965924853, -0.970294813, -0.974369212, -0.97814681, -0.981626455, -0.984807087, -0.987687739, -0.990267531, -0.992545679, -0.994521489, -0.996194358, -0.997563777, -0.998629329, -0.999390689, -0.999847626 -1.0, -0.999847765, -0.999390967, -0.998629745, -0.997564332, -0.996195051, -0.994522321, -0.992546649, -0.990268639, -0.987688984, -0.98480847, -0.981627974, -0.978148465, -0.974371003, -0.970296739, -0.965926914, -0.961262858, -0.956305993, -0.951057828, -0.945519963, -0.939694083, -0.933581964, -0.927185467, -0.920506541, -0.913547221, -0.906309625, -0.898795959, -0.891008512, -0.882949655, -0.874621844, -0.866027615, -0.857169586, -0.848050455, -0.838673001, -0.829040079, -0.819154623, -0.809019646, -0.798638234, -0.788013549, -0.777148828, -0.766047381, -0.754712588, -0.743147903, -0.731356849, -0.719343016, -0.707110065, -0.694661722, -0.682001778, -0.66913409, -0.656062578, -0.642791223, -0.629324069, -0.615665216, -0.601818826, -0.587789117, -0.573580361, -0.559196888, -0.544643078, -0.529923365, -0.515042232, -0.500004213, -0.484813888, -0.469475884, -0.453994874, -0.438375572, -0.422622738, -0.406741168, -0.390735702, -0.374611213, -0.358372615, -0.342024853, -0.325572908, -0.309021789, -0.29237654, -0.275642231, -0.258823958, -0.241926845, -0.224956039, -0.207916709, -0.190814046, -0.173653259, -0.156439576, -0.13917824, -0.121874509, -0.104533653, -0.087160956, -0.069761709, -0.052341212, -0.034904771, -0.017457698 }; //Cosine table used for circle drawing: flash double cos_table[360]={ 1.0, 0.999847695, 0.999390828, 0.998629537, 0.997564054, 0.996194705, 0.994521905, 0.992546164, 0.990268085, 0.987688361, 0.984807779, 0.981627214, 0.978147638, 0.974370108, 0.970295776, 0.965925884, 0.961261761, 0.956304829, 0.951056598, 0.945518667, 0.939692722, 0.933580537, 0.927183976, 0.920504986, 0.913545602, 0.906307943, 0.898794214, 0.891006705, 0.882947787, 0.874619914, 0.866025625, 0.857167536, 0.848048346, 0.838670833, 0.829037853, 0.81915234, 0.809017306, 0.798635838, 0.788011099, 0.777146323, 0.766044822, 0.754709977, 0.74314524, 0.731354134, 0.719340251, 0.70710725, 0.694658858, 0.681998867, 0.669131132, 0.656059574, 0.642788174, 0.629320975, 0.615662079, 0.601815647, 0.587785896, 0.573577101, 0.559193588, 0.54463974, 0.529919989, 0.51503882, 0.500000766, 0.484810407, 0.46947237, 0.453991327, 0.438371995, 0.42261913, 0.406737532, 0.390732038, 0.374607523, 0.358368899, 0.342021113, 0.325569144, 0.309018004, 0.292372734, 0.275638404, 0.258820113, 0.241922983, 0.22495216, 0.207912816, 0.190810139, 0.173649339, 0.156435644, 0.139174298, 0.121870558, 0.104529695, 0.087156991, 0.069757738, 0.052337237, 0.034900793, 0.017453718, 0.0, -0.017451065, -0.034898141, -0.052334587, -0.069755091, -0.087154348, -0.104527056, -0.121867924, -0.13917167, -0.156433024, -0.173646726, -0.190807534, -0.20791022, -0.224949575, -0.241920408, -0.25881755, -0.275635854, -0.292370196, -0.30901548, -0.325566635, -0.342018619, -0.358366422, -0.374605063, -0.390729595, -0.406735108, -0.422616725, -0.43836961, -0.453988963, -0.469470027, -0.484808086, -0.499998468, -0.515036546, -0.529917739, -0.544637514, -0.559191388, -0.573574927, -0.58778375, -0.601813528, -0.615659988, -0.629318913, -0.642786142, -0.656057571, -0.66912916, -0.681996926, -0.694656949, -0.707105374, -0.719338408, -0.731352324, -0.743143464, -0.754708236, -0.766043116, -0.777144653, -0.788009465, -0.798634241, -0.809015747, -0.819150818, -0.829036369, -0.838669388, -0.84804694, -0.857166169, -0.866024298, -0.874618628, -0.882946541, -0.8910055, -0.898793051, -0.906306821, -0.913544522, -0.920503949, -0.927182982, -0.933579586, -0.939691814, -0.945517803, -0.951055778, -0.956304053, -0.96126103, -0.965925197, -0.970295134, -0.974369511, -0.978147086, -0.981626708, -0.984807318, -0.987687946, -0.990267716, -0.992545841, -0.994521627, -0.996194473, -0.997563869, -0.998629398, -0.999390735, -0.999847649 -1.0, -0.999847742, -0.999390921, -0.998629676, -0.997564239, -0.996194936, -0.994522182, -0.992546488, -0.990268454, -0.987688776, -0.984808239, -0.981627721, -0.978148189, -0.974370705, -0.970296418, -0.96592657, -0.961262492, -0.956305605, -0.951057418, -0.945519531, -0.939693629, -0.933581488, -0.92718497, -0.920506023, -0.913546681, -0.906309064, -0.898795378, -0.89100791, -0.882949032, -0.874621201, -0.866026952, -0.857168903, -0.848049752, -0.838672278, -0.829039337, -0.819153862, -0.809018866, -0.798637435, -0.788012732, -0.777147993, -0.766046528, -0.754711718, -0.743147015, -0.731355944, -0.719342094, -0.707109127, -0.694660767, -0.682000808, -0.669133104, -0.656061577, -0.642790207, -0.629323038, -0.61566417, -0.601817766, -0.587788043, -0.573579274, -0.559195788, -0.544641965, -0.52992224, -0.515041095, -0.500003064, -0.484812728, -0.469474713, -0.453993692, -0.43837438, -0.422621535, -0.406739956, -0.39073448, -0.374609983, -0.358371377, -0.342023607, -0.325571653, -0.309020528, -0.292375272, -0.275640955, -0.258822676, -0.241925557, -0.224954746, -0.207915411, -0.190812743, -0.173651952, -0.156438265, -0.139176926, -0.121873192, -0.104532334, -0.087159635, -0.069760386, -0.052339887, -0.034903445, -0.017456371, 0.0, 0.017448412, 0.034895489, 0.052331937, 0.069752444, 0.087151704, 0.104524417, 0.12186529, 0.139169043, 0.156430403, 0.173644113, 0.190804929, 0.207907624, 0.224946989, 0.241917833, 0.258814987, 0.275633303, 0.292367659, 0.309012956, 0.325564126, 0.342016126, 0.358363945, 0.374602602, 0.390727152, 0.406732684, 0.42261432, 0.438367225, 0.453986599, 0.469467684, 0.484805765, 0.49999617, 0.515034271, 0.529915489, 0.544635289, 0.559189188, 0.573572753, 0.587781603, 0.601811409, 0.615657897, 0.629316851, 0.642784109, 0.656055569, 0.669127188, 0.681994985, 0.694655041, 0.707103498, 0.719336564, 0.731350514, 0.743141689, 0.754706495, 0.766041411, 0.777142983, 0.788007831, 0.798632644, 0.809014187, 0.819149296, 0.829034885, 0.838667942, 0.848045534, 0.857164803, 0.866022971, 0.874617341, 0.882945295, 0.891004295, 0.898791888, 0.9063057, 0.913543443, 0.920502912, 0.927181988, 0.933578636, 0.939690906, 0.945516939, 0.951054958, 0.956303278, 0.961260298, 0.96592451, 0.970294492, 0.974368914, 0.978146534, 0.981626202, 0.984806857, 0.987687531, 0.990267347, 0.992545517, 0.99452135, 0.996194242, 0.997563684, 0.998629259, 0.999390643, 0.999390643 }; //Point plot lookup table //One bit masks flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //================================ //3x5 font numbers, then letters //packed two per definition for fast //copy to the screen at x-position divisible by 4 flash char smallbitmap[39][5]={ //0 0b11101110, 0b10101010, 0b10101010, 0b10101010, 0b11101110, //1 0b01000100, 0b11001100, 0b01000100, 0b01000100, 0b11101110, //2 0b11101110, 0b00100010, 0b11101110, 0b10001000, 0b11101110, //3 0b11101110, 0b00100010, 0b11101110, 0b00100010, 0b11101110, //4 0b10101010, 0b10101010, 0b11101110, 0b00100010, 0b00100010, //5 0b11101110, 0b10001000, 0b11101110, 0b00100010, 0b11101110, //6 0b11001100, 0b10001000, 0b11101110, 0b10101010, 0b11101110, //7 0b11101110, 0b00100010, 0b01000100, 0b10001000, 0b10001000, //8 0b11101110, 0b10101010, 0b11101110, 0b10101010, 0b11101110, //9 0b11101110, 0b10101010, 0b11101110, 0b00100010, 0b01100110, //: 0b00000000, 0b01000100, 0b00000000, 0b01000100, 0b00000000, //= 0b00000000, 0b11101110, 0b00000000, 0b11101110, 0b00000000, //blank 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, //A 0b11101110, 0b10101010, 0b11101110, 0b10101010, 0b10101010, //B 0b11001100, 0b10101010, 0b11101110, 0b10101010, 0b11001100, //C 0b11101110, 0b10001000, 0b10001000, 0b10001000, 0b11101110, //D 0b11001100, 0b10101010, 0b10101010, 0b10101010, 0b11001100, //E 0b11101110, 0b10001000, 0b11101110, 0b10001000, 0b11101110, //F 0b11101110, 0b10001000, 0b11101110, 0b10001000, 0b10001000, //G 0b11101110, 0b10001000, 0b10001000, 0b10101010, 0b11101110, //H 0b10101010, 0b10101010, 0b11101110, 0b10101010, 0b10101010, //I 0b11101110, 0b01000100, 0b01000100, 0b01000100, 0b11101110, //J 0b00100010, 0b00100010, 0b00100010, 0b10101010, 0b11101110, //K 0b10001000, 0b10101010, 0b11001100, 0b11001100, 0b10101010, //L 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b11101110, //M 0b10101010, 0b11101110, 0b11101110, 0b10101010, 0b10101010, //N 0b00000000, 0b11001100, 0b10101010, 0b10101010, 0b10101010, //O 0b01000100, 0b10101010, 0b10101010, 0b10101010, 0b01000100, //P 0b11101110, 0b10101010, 0b11101110, 0b10001000, 0b10001000, //Q 0b01000100, 0b10101010, 0b10101010, 0b11101110, 0b01100110, //R 0b11101110, 0b10101010, 0b11001100, 0b11101110, 0b10101010, //S 0b11101110, 0b10001000, 0b11101110, 0b00100010, 0b11101110, //T 0b11101110, 0b01000100, 0b01000100, 0b01000100, 0b01000100, //U 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b11101110, //V 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b01000100, //W 0b10101010, 0b10101010, 0b11101110, 0b11101110, 0b10101010, //X 0b00000000, 0b10101010, 0b01000100, 0b01000100, 0b10101010, //Y 0b10101010, 0b10101010, 0b01000100, 0b01000100, 0b01000100, //Z 0b11101110, 0b00100010, 0b01000100, 0b10001000, 0b11101110 }; //================================== //This is the sync generator and raster generator. It MUST be entered from //sleep mode to get accurate timing of the sync pulses #pragma warn- interrupt [TIM1_COMPA] void t1_cmpA(void) begin //start the Horizontal sync pulse PORTD = syncON; //update the curent scanline number LineCount ++ ; //begin inverted (Vertical) synch after line 247 if (LineCount==248) begin syncON = 0b00100000; syncOFF = 0; end //back to regular sync after line 250 if (LineCount==251) begin syncON = 0; syncOFF = 0b00100000; end //start new frame after line 262 if (LineCount==263) begin LineCount = 1; end delay_us(2); //adjust to make 5 us pulses //end sync pulse PORTD = syncOFF; if (LineCount=ScreenTop) begin //compute byte index for beginning of the next line //left-shift 4 would be individual lines // <<3 means line-double the pixels //The 0xfff8 truncates the odd line bit //i=(LineCount-ScreenTop)<<3 & 0xfff8; // #asm push r16 lds r12, _LineCount lds r13, _Linecount+1 ldi r16, 30 sub r12, r16 ldi r16,0 sbc r13, r16 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 mov r16,r12 andi r16,0xf0 mov r12,r16 pop r16 #endasm //load 16 registers with screen info #asm push r14 push r15 push r16 push r17 push r18 push r19 push r26 push r27 ldi r26,low(_screen) ;base address of screen ldi r27,high(_screen) add r26,r12 ;offset into screen (add i) adc r27,r13 ld r4,x+ ;load 16 registers and inc pointer ld r5,x+ ld r6,x+ ld r7,x+ ld r8,x+ ld r9,x+ ld r10,x+ ld r11,x+ ld r12,x+ ld r13,x+ ld r14,x+ ld r15,x+ ld r16,x+ ld r17,x+ ld r18,x+ ld r19,x pop r27 pop r26 #endasm delay_us(4); //adjust to center image on screen //blast 16 bytes to the screen #asm ;but first a macro to make the code shorter ;the macro takes a register number as a parameter ;and dumps its bits serially to portD.6 ;the nop can be eliminated to make the display narrower .macro videobits ;regnum BST @0,7 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,6 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,5 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,4 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,3 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,2 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,1 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,0 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 .endm videobits r4 ;video line -- byte 1 videobits r5 ;byte 2 videobits r6 ;byte 3 videobits r7 ;byte 4 videobits r8 ;byte 5 videobits r9 ;byte 6 videobits r10 ;byte 7 videobits r11 ;byte 8 videobits r12 ;byte 9 videobits r13 ;byte 10 videobits r14 ;byte 11 videobits r15 ;byte 12 videobits r16 ;byte 13 videobits r17 ;byte 14 videobits r18 ;byte 15 videobits r19 ;byte 16 clt ;clear video after the last pixel on the line IN R30,0x12 BLD R30,6 OUT 0x12,R30 pop r19 pop r18 pop r17 pop r16 pop r15 pop r14 #endasm end //Receive UART signals from mouse mcu: if(UCSRA.7) //Receive Complete begin temp=getchar(); UART_buffer[r_index++]=temp; //Add character to buffer if(temp==0xff)//Stop character begin cmd_ready=1; r_index=0; end end end #pragma warn+ //================================== //plot one point //at x,y with color 1=white 0=black 2=invert #pragma warn- void video_pt(char x, char y, char c) begin //if statement/x_draw_limit prevents user from drawing on tool bar area if(x127 || y < 0 || y > 99) return; #asm ; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it push r16 ldd r30,y+2 ;get x lsr r30 lsr r30 lsr r30 ;divide x by 8 ldd r12,y+1 ;get y lsl r12 ;mult y by 16 clr r13 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 add r12, r30 ;add in x/8 ;v2 = screen[i]; r5 ;v3 = pos[x & 7]; r6 ;v4 = c r7 ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 ld r5,Z ;get screen byte ldd r26,y+2 ;get x ldi r27,0 andi r26,0x07 ;form x & 7 ldi r30,low(_pos*2) ldi r31,high(_pos*2) add r30,r26 adc r31,r27 lpm r6,Z ld r16,y ;get c ;if (v4==1) screen[i] = v2 | v3 ; ;if (v4==0) screen[i] = v2 & ~v3; ;if (v4==2) screen[i] = v2 ^ v3 ; cpi r16,1 brne tst0 or r5,r6 tst0: cpi r16,0 brne tst2 com r6 and r5,r6 tst2: cpi r16,2 brne writescrn eor r5,r6 writescrn: ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 st Z, r5 ;write the byte back to the screen pop r16 #endasm end #pragma warn+ //================================== // put a small character on the screen // x-cood must be on divisible by 4 // c is index into bitmap void video_smallchar(char x, char y, char c) begin char mask; i=((int)x>>3) + ((int)y<<4) ; if (x == (x & 0xf8)) mask = 0x0f; //f8 else mask = 0xf0; screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask); screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask); screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask); screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask); screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask); end //================================== // put a string of small characters on the screen // x-cood must be on divisible by 4 void video_putsmalls(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_smallchar(x,y,str[i]-0x30); else video_smallchar(x,y,str[i]-0x40+12); x = x+4; end end //================================== //plot a line //at x1,y1 to x2,y2 with color 1=white 0=black 2=invert //NOTE: this function requires signed chars //Code is from David Rodgers, //"Procedural Elements of Computer Graphics",1985 void video_line(char x1, char y1, char x2, char y2, char c) begin int e; signed char dx,dy,j, temp; signed char s1,s2, xchange; signed char x,y; x = x1; y = y1; dx = cabs(x2-x1); dy = cabs(y2-y1); s1 = csign(x2-x1); s2 = csign(y2-y1); xchange = 0; if (dy>dx) begin temp = dx; dx = dy; dy = temp; xchange = 1; end e = ((int)dy<<1) - dx; for (j=0; j<=dx; j++) begin video_pt(x,y,c) ; if (e>=0) begin if (xchange==1) x = x + s1; else y = y + s2; e = e - ((int)dx<<1); end if (xchange==1) y = y + s2; else x = x + s1; e = e + ((int)dy<<1); end end //================================== //return the value of one point //at x,y with color 1=white 0=black 2=invert char video_set(char x, char y) begin //The following construction //detects exactly one bit at the x,y location i=((int)x>>3) + ((int)y<<4) ; return ( screen[i] & 1<<(7-(x & 0x7))); end //================================== // put an icon from the bitmap on screen, using an opaque mask // c is index into bitmap void video_puticon(char x, char y, char c) begin v7 = x; for (v6=0;v6<7;v6++) begin v1 = icon_bitmap[c][v6]; v8 = y+v6; video_pt(v7, v8, (v1 & 0x80)==0x80); video_pt(v7+1, v8, (v1 & 0x40)==0x40); video_pt(v7+2, v8, (v1 & 0x20)==0x20); video_pt(v7+3, v8, (v1 & 0x10)==0x10); video_pt(v7+4, v8, (v1 & 0x08)==0x08); video_pt(v7+5, v8, (v1 & 0x04)==0x04); video_pt(v7+6, v8, (v1 & 0x02)==0x02); video_pt(v7+7, v8, (v1 & 0x01)==0x01); end end //================================== // put an icon from the bitmap on screen, using an transparent mask // c is index into bitmap void video_putspray(char x, char y, char c) begin v7 = x; for (v6=0;v6<7;v6++) begin v1 = icon_bitmap[c][v6]; v8 = y+v6; //if statements are used to transparently draw over pixels if((v1 & 0x80) == 0x80) video_pt(v7, v8, 1); if((v1 & 0x40) == 0x40) video_pt(v7+1, v8, 1); if((v1 & 0x20) == 0x20) video_pt(v7+2, v8, 1); if((v1 & 0x10) == 0x10) video_pt(v7+3, v8, 1); if((v1 & 0x08) == 0x08) video_pt(v7+4, v8, 1); if((v1 & 0x04) == 0x04) video_pt(v7+5, v8, 1); if((v1 & 0x02) == 0x02) video_pt(v7+6, v8, 1); if((v1 & 0x01) == 0x01) video_pt(v7+7, v8, 1); end end //================================== initialize function void initialize(void) begin #asm ("cli"); //init timer 1 to generate sync OCR1A = lineTime; //One NTSC line TCCR1B = 9; //full speed; clear-on-match TCCR1A = 0x00; //turn off pwm and oc lines TIMSK = 0x10; //enable interrupt T1 cmp //init ports DDRD = 0xf0; //video out and switches DDRB = 0xff; PORTB=0xff; //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; //init UART: UCSRB = 0x10 + 0x08 ;//enable receiver and transmitter UBRRL = 16; //57600 bps UBRRH = 0; UART_buffer[0]=0x00; cmd_ready=0; r_index=0; //draw lines on screen: #define width 126 video_line(0,0,0,99,1); video_line(width,0,width,99,1); //top line & bottom lines video_line(0,0,width,0,1); video_line(0,99,width,99,1); //Draw the toolbar and icons: video_line(10,0,10,80,1); { unsigned char i; for(i=0;i<9;i++) begin video_line(0,i*10,10,i*10,1); if(i<8) video_puticon(2, 2+i*10, i); end } x_draw_limit = 10; video_putsmalls(4,84,toolname[cur_tool]); video_line(10,80,25,80,1); video_line(25,80,25,99,1); //init software timer t=0; time=0; //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); end //================================== // set up the ports and timers void main(void) begin initialize(); //The following loop executes once/video line during lines //1-230, then does all of the frame-end processing while(1) begin //stall here until next line starts //sleep enable; mode=idle //use sleep to make entry into sync ISR uniform time #asm ("sleep"); //The following code executes during the vertical blanking //Code here can be as long as //a total of 60 lines x 63.5 uSec/line x 8 cycles/uSec if (LineCount==231) begin if (cmd_ready) //check to see if the command from mouse mcu has been completely received begin cmd_ready=0; r_index=0; //Reset pointer location of UART_buffer //Check if signal over UART matches the syntax for a cursor update: if (UART_buffer[0]==UART_UPDATE_CURSOR && UART_buffer[6]==0xff) begin //store cursor locations last_cursor_x = UART_buffer[4]; last_cursor_y = UART_buffer[5]; cursor_x = UART_buffer[1];//horzontal comp cursor_y = UART_buffer[2];//vertical comp //erase the cursor point drawn in last frame, unless it has written with paint if( video_set(last_cursor_x,last_cursor_y) != last_cursor_c ) begin x_draw_limit = 0; video_pt(last_cursor_x,last_cursor_y,2) ; x_draw_limit = 10; end if((UART_buffer[3]&3)==0b00)//mouse button not pushed begin button_dragging = 0; button_up = 0; button_down = 0; PORTB.0=0; //debugging leds PORTB.1=1; PORTB.2=1; PORTB.3=1; end else if((UART_buffer[3]&3)==0b01) //button initially down(occurs once per click) begin button_dragging = 0; button_up = 0; button_down = 1; //if line,rect or oval tool store click location to draw correct shape if(cur_tool == TOOL_LINE) begin click_origin_x = cursor_x; click_origin_y = cursor_y; end else if(cur_tool == TOOL_RECT) begin click_origin_x = cursor_x; click_origin_y = cursor_y; end else if(cur_tool == TOOL_OVAL) begin click_origin_x = cursor_x; click_origin_y = cursor_y; end PORTB.0=1; //debugging leds PORTB.1=0; PORTB.2=1; PORTB.3=1; end else if((UART_buffer[3]&3)==0b10) //mouse buttton down and being dragged begin button_dragging = 1; button_up = 0; button_down = 0; //Draw a point if(cur_tool == TOOL_POINT) begin video_pt(cursor_x,cursor_y,1) ; end else if(cur_tool == TOOL_LINE) begin //erase line from last frame, and draw new line, allows interactive line drawing video_line(click_origin_x,click_origin_y,last_cursor_x,last_cursor_y,2); video_line(click_origin_x,click_origin_y,cursor_x,cursor_y,2); end else if(cur_tool == TOOL_SPRAY) begin //draw a spray icon and advance spray bitmap frame: video_putspray(cursor_x-3, cursor_y-3, spray_frame+8); spray_frame++; if(spray_frame == 7) spray_frame = 0; end else if(cur_tool == TOOL_FILL) //Actually the brush tool begin //paint an icon of a brush/circle video_putspray(cursor_x-3, cursor_y-3, 4); end else if(cur_tool == TOOL_ERASER) begin //Erase points around cursor loc: video_pt(cursor_x,cursor_y,0) ; video_pt(cursor_x,cursor_y+1,0) ; video_pt(cursor_x,cursor_y-1,0) ; video_pt(cursor_x+1,cursor_y,0) ; video_pt(cursor_x-1,cursor_y,0) ; end else if(cur_tool == TOOL_RECT) begin unsigned char mid_x,mid_y,last_mid_x,last_mid_y; //Calculate positions of points that trace out rectangle frame: mid_x = (unsigned char)((char)((char)(cursor_x - click_origin_x)/2 + click_origin_x)); mid_y = (unsigned char)((char)((char)(cursor_y - click_origin_y)/2 + click_origin_y)); last_mid_x = (unsigned char)((char)((char)(last_cursor_x - click_origin_x)/2 + click_origin_x)); last_mid_y = (unsigned char)((char)((char)(last_cursor_y - click_origin_y)/2 + click_origin_y)); //erase rectangle frame from last screen update video_pt(last_cursor_x,last_cursor_y,2) ; video_pt(click_origin_x,click_origin_y,2) ; video_pt(last_cursor_x,click_origin_y,2) ; video_pt(click_origin_x,last_cursor_y,2) ; video_pt(last_cursor_x,last_mid_y,2) ; video_pt(click_origin_x,last_mid_y,2) ; video_pt(last_mid_x,click_origin_y,2) ; video_pt(last_mid_x,last_cursor_y,2) ; //draw rectangle frame for next screen update video_pt(cursor_x,cursor_y,2) ; video_pt(click_origin_x,click_origin_y,2) ; video_pt(cursor_x,click_origin_y,2) ; video_pt(click_origin_x,cursor_y,2) ; video_pt(cursor_x,mid_y,2) ; video_pt(click_origin_x,mid_y,2) ; video_pt(mid_x,click_origin_y,2) ; video_pt(mid_x,cursor_y,2) ; end else if(cur_tool == TOOL_OVAL) begin unsigned int i; //Calculate positions of points that trace out circle frame: rad_x = fabs((char)(cursor_x - click_origin_x)/2); rad_y = fabs((char)(cursor_y - click_origin_y)/2); mid_x = (unsigned char)((char)((char)(cursor_x - click_origin_x)/2 + click_origin_x)); mid_y = (unsigned char)((char)((char)(cursor_y - click_origin_y)/2 + click_origin_y)); last_rad_x = fabs((char)(last_cursor_x - click_origin_x)/2); last_rad_y = fabs((char)(last_cursor_y - click_origin_y)/2); last_mid_x = (unsigned char)((char)((char)(last_cursor_x - click_origin_x)/2 + click_origin_x)); last_mid_y = (unsigned char)((char)((char)(last_cursor_y - click_origin_y)/2 + click_origin_y)); //erase oval frame from last screen update: for(i=0; i < 360; i+=45) begin video_pt(last_mid_x + last_rad_x*sin_table[i], last_mid_y + last_rad_y*cos_table[i],2) ; end //erase oval frame for next screen update: for(i=0; i < 360; i+=45) begin video_pt(mid_x + rad_x*sin_table[i], mid_y + rad_y*cos_table[i],2) ; end end PORTB.0=1; //debugging leds PORTB.1=1; PORTB.2=0; PORTB.3=1; end else if((UART_buffer[3]&3)==0b11 && !button_up && button_dragging) //button released, draw final shape begin button_dragging = 0; button_up = 1; button_down = 0; if(cur_tool == TOOL_LINE) begin //erase interactive line and draw final paint video_line(click_origin_x,click_origin_y,last_cursor_x,last_cursor_y,2); video_line(click_origin_x,click_origin_y,cursor_x,cursor_y,1); end else if(cur_tool == TOOL_RECT) begin unsigned char last_mid_x,last_mid_y;; last_mid_x = (unsigned char)((char)((char)(last_cursor_x - click_origin_x)/2 + click_origin_x)); last_mid_y = (unsigned char)((char)((char)(last_cursor_y - click_origin_y)/2 + click_origin_y)); //erase interactive reect frame and draw final paint video_pt(last_cursor_x,last_cursor_y,2) ; video_pt(click_origin_x,click_origin_y,2) ; video_pt(last_cursor_x,click_origin_y,2) ; video_pt(click_origin_x,last_cursor_y,2) ; video_pt(last_cursor_x,last_mid_y,2) ; video_pt(click_origin_x,last_mid_y,2) ; video_pt(last_mid_x,click_origin_y,2) ; video_pt(last_mid_x,last_cursor_y,2) ; //draw final paint : video_line(click_origin_x,click_origin_y,cursor_x,click_origin_y,1); video_line(click_origin_x,click_origin_y,click_origin_x,cursor_y,1); video_line(cursor_x,click_origin_y,cursor_x,cursor_y,1); video_line(click_origin_x,cursor_y,cursor_x,cursor_y,1); end else if(cur_tool == TOOL_OVAL) begin unsigned int i; #asm("cli"); //disable screen refresh rad_x = fabs((char)(cursor_x - click_origin_x)/2); rad_y = fabs((char)(cursor_y - click_origin_y)/2); mid_x = (unsigned char)((char)((char)(cursor_x - click_origin_x)/2 + click_origin_x)); mid_y = (unsigned char)((char)((char)(cursor_y - click_origin_y)/2 + click_origin_y)); last_rad_x = fabs((char)(last_cursor_x - click_origin_x)/2); last_rad_y = fabs((char)(last_cursor_y - click_origin_y)/2); last_mid_x = (unsigned char)((char)((char)(last_cursor_x - click_origin_x)/2 + click_origin_x)); last_mid_y = (unsigned char)((char)((char)(last_cursor_y - click_origin_y)/2 + click_origin_y)); //erase interactive oval frame and draw final paint for(i=0; i < 360; i+=45) begin video_pt(last_mid_x + last_rad_x*sin_table[i], last_mid_y + last_rad_y*cos_table[i],2) ; end //draw final circle for(i=0; i < 360; i+=1) begin unsigned char cx,cy; cx = mid_x + rad_x*cos_table[i]; cy = mid_y + rad_y*sin_table[i]; //eliminate angles which cause bogus points probably due to precision ertors if(i!=359 && i !=269 && i !=179) begin video_pt(cx, cy,1) ; //draw each point in circle end end #asm("sei"); //enable screen refresh end PORTB.0=1; //debugging leds PORTB.1=1; PORTB.2=1; PORTB.3=0; end //update current tool if person clicked on toolbar if( UART_buffer[3] & 4) begin unsigned char new_tool; new_tool = ((UART_buffer[3] & 0b00111000)>>3); //if tool is clear screen dont update current tool but erase drawing area: if(new_tool == TOOL_CLEAR) begin unsigned char ix,iy; #asm("cli"); for(ix = 11; ix < 26; ix++) begin for(iy = 1; iy < 80; iy++) begin video_pt(ix,iy,0) ; end end for(ix = 26; ix < 126; ix++) begin for(iy = 1; iy < 99; iy++) begin video_pt(ix,iy,0) ; end end #asm("sei"); end else cur_tool = ((UART_buffer[3] & 0b00111000)>>3); video_putsmalls(4,84,toolname[cur_tool]); end if(cur_tool == TOOL_OVAL && (UART_buffer[3]&3)==0b11) begin last_cursor_c=0;//special handling for circle tool end else begin //store pixel color before drawing cursor: last_cursor_c = video_set(cursor_x,cursor_y); x_draw_limit = 0; //Draw cursor point: video_pt(cursor_x,cursor_y,2) ; x_draw_limit = 10; end end end end //line 231 end //while end //main