-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvga.c
More file actions
390 lines (304 loc) · 9.29 KB
/
vga.c
File metadata and controls
390 lines (304 loc) · 9.29 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define BUFFER_WIDTH 512
volatile int *pixel_ctrl_ptr = (int *)0xFF203020;
volatile int *ps2_ptr = (int *)0xFF200100;
short int Buffer1[SCREEN_HEIGHT][BUFFER_WIDTH];
short int Buffer2[SCREEN_HEIGHT][BUFFER_WIDTH];
volatile int pixel_buffer_start;
#define DELAY_COUNT 1
// Function prototypes
void delay_ms(int ms);
void clear_screen();
void vSync();
void plot_pixel(int x, int y, short int color);
void draw_char(int x, int y, char c, short color);
void draw_string(int x, int y, const char *str, short color);
void update_key_state(unsigned char code, bool pressed);
// New helper functions
void draw_circle(int cx, int cy, int radius, short color);
void draw_line_thick(int x0, int y0, int x1, int y1, short color, int thickness);
void draw_robot_arm(int timers[5]);
static const unsigned char font_digits[10][7] = {
{0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E},
{0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E},
{0x0E, 0x11, 0x01, 0x06, 0x08, 0x10, 0x1F},
{0x1F, 0x02, 0x04, 0x02, 0x01, 0x11, 0x0E},
{0x02, 0x06, 0x0A, 0x12, 0x1F, 0x02, 0x02},
{0x1F, 0x10, 0x1E, 0x01, 0x01, 0x11, 0x0E},
{0x0E, 0x10, 0x1E, 0x11, 0x11, 0x11, 0x0E},
{0x1F, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08},
{0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E},
{0x0E, 0x11, 0x11, 0x0F, 0x01, 0x11, 0x0E}
};
bool key_pressed[10] = { false, false, false, false, false, false, false, false, false, false };
void update_key_state(unsigned char code, bool pressed)
{
switch(code)
{
case 0x1C: // 'A'
key_pressed[0] = pressed;
break;
case 0x1A: // 'Z'
key_pressed[1] = pressed;
break;
case 0x1B: // 'S'
key_pressed[2] = pressed;
break;
case 0x22: // 'X'
key_pressed[3] = pressed;
break;
case 0x23: // 'D'
key_pressed[4] = pressed;
break;
case 0x21: // 'C'
key_pressed[5] = pressed;
break;
case 0x2B: // 'F'
key_pressed[6] = pressed;
break;
case 0x2A: // 'V'
key_pressed[7] = pressed;
break;
case 0x34: // 'G'
key_pressed[8] = pressed;
break;
case 0x32: // 'B'
key_pressed[9] = pressed;
break;
default:
break;
}
}
void draw_circle(int cx, int cy, int radius, short color)
{
for (int dy = -radius; dy <= radius; dy++)
{
for (int dx = -radius; dx <= radius; dx++)
{
if (dx*dx + dy*dy <= radius*radius)
{
plot_pixel(cx + dx, cy + dy, color);
}
}
}
}
void draw_line_thick(int x0, int y0, int x1, int y1, short color, int thickness)
{
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
int cx = x0;
int cy = y0;
while (true)
{
// Draw a small filled circle at (cx, cy) for thickness
draw_circle(cx, cy, thickness, color);
if (cx == x1 && cy == y1) break;
int e2 = 2 * err;
if (e2 > -dy) { err -= dy; cx += sx; }
if (e2 < dx) { err += dx; cy += sy; }
}
}
void draw_robot_arm(int timers[5])
{
// Base position near the bottom center of the screen
int base_x = SCREEN_WIDTH / 2;
int base_y = SCREEN_HEIGHT - 20;
for (int y = base_y; y < SCREEN_HEIGHT; y++)
{
for (int x = base_x - 30; x <= base_x + 30; x++)
{
plot_pixel(x, y, 0x7BEF); // a grayish color
}
}
draw_circle(base_x, base_y, 8, 0xF800); // red pivot
// Segment lengths
int seg_lengths[4] = {50, 40, 30, 20};
double cumulative_angle = -90.0 + (timers[0] - 90);
double x0 = base_x;
double y0 = base_y;
double x1, y1;
short segment_color = 0x07E0;
short joint_color = 0xFFE0; // yellow
for (int i = 0; i < 4; i++)
{
double rad = cumulative_angle * M_PI / 180.0;
x1 = x0 + seg_lengths[i] * cos(rad);
y1 = y0 + seg_lengths[i] * sin(rad);
draw_line_thick((int)x0, (int)y0, (int)x1, (int)y1, segment_color, 3);
draw_circle((int)x1, (int)y1, 5, joint_color);
x0 = x1;
y0 = y1;
if (i < 3)
cumulative_angle += (timers[i + 1] - 90);
}
double claw_angle_deg = cumulative_angle;
double claw_angle_rad = claw_angle_deg * M_PI / 180.0;
double max_spread = 40.0;
double spread = (timers[4] / 180.0) * max_spread;
double left_angle = claw_angle_deg + spread;
double right_angle = claw_angle_deg - spread;
int claw_length = 15;
// compute endpoints
double rad_left = left_angle * M_PI / 180.0;
double rad_right = right_angle * M_PI / 180.0;
int claw_x1 = (int)(x0 + claw_length * cos(rad_left));
int claw_y1 = (int)(y0 + claw_length * sin(rad_left));
int claw_x2 = (int)(x0 + claw_length * cos(rad_right));
int claw_y2 = (int)(y0 + claw_length * sin(rad_right));
short claw_color = 0x001F; // blue
draw_line_thick((int)x0, (int)y0, claw_x1, claw_y1, claw_color, 2);
draw_line_thick((int)x0, (int)y0, claw_x2, claw_y2, claw_color, 2);
draw_circle(claw_x1, claw_y1, 3, 0xFFFF);
draw_circle(claw_x2, claw_y2, 3, 0xFFFF);
}
int main(void)
{
*(pixel_ctrl_ptr + 1) = (int)&Buffer1;
vSync();
pixel_buffer_start = *pixel_ctrl_ptr;
clear_screen();
*(pixel_ctrl_ptr + 1) = (int)&Buffer2;
pixel_buffer_start = *(pixel_ctrl_ptr + 1);
clear_screen();
int textX = 10;
int timer_y_positions[5] = {20, 50, 80, 110, 140};
int timers[5] = {90, 90, 90, 90, 90};
int holdCounterIncrease[5] = {0,0,0,0,0};
int holdCounterDecrease[5] = {0,0,0,0,0};
int normalIncrement = 1;
int acceleratedIncrement = 10;
int normalDecrement = 1;
int acceleratedDecrement = 10;
const int accelerationThreshold = 20;
bool release_flag = false;
while (1)
{
int ps2_data = *ps2_ptr;
if (ps2_data & 0x8000)
{
unsigned char code = ps2_data & 0xFF;
if (code == 0xF0)
{
release_flag = true;
}
else
{
if (release_flag)
{
update_key_state(code, false);
release_flag = false;
}
else
{
update_key_state(code, true);
}
}
}
for (int i = 0; i < 5; i++)
{
if (key_pressed[2*i])
{
holdCounterIncrease[i]++;
int inc = (holdCounterIncrease[i] > accelerationThreshold)
? acceleratedIncrement
: normalIncrement;
timers[i] += inc;
if (timers[i] > 180) timers[i] = 180;
}
else
{
holdCounterIncrease[i] = 0;
}
if (key_pressed[2*i + 1])
{
holdCounterDecrease[i]++;
int dec = (holdCounterDecrease[i] > accelerationThreshold)
? acceleratedDecrement
: normalDecrement;
timers[i] -= dec;
if (timers[i] < 0) timers[i] = 0;
}
else
{
holdCounterDecrease[i] = 0;
}
}
clear_screen();
for (int i = 0; i < 5; i++)
{
char textBuffer[16];
sprintf(textBuffer, "T%d: %d", i+1, timers[i]);
draw_string(textX, timer_y_positions[i], textBuffer, 0xFFFF);
}
draw_robot_arm(timers);
vSync();
pixel_buffer_start = *(pixel_ctrl_ptr + 1);
delay_ms(10);
}
return 0;
}
void delay_ms(int ms)
{
volatile int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < DELAY_COUNT; j++)
;
}
void vSync()
{
*pixel_ctrl_ptr = 1;
while ((*(pixel_ctrl_ptr + 3) & 0x01) != 0)
;
}
void clear_screen()
{
int x, y;
volatile short int *pixel_ptr = (volatile short int *)pixel_buffer_start;
for (y = 0; y < SCREEN_HEIGHT; y++)
{
for (x = 0; x < SCREEN_WIDTH; x++)
{
pixel_ptr[y * BUFFER_WIDTH + x] = 0;
}
}
}
void plot_pixel(int x, int y, short int color)
{
if (x < 0 || x >= SCREEN_WIDTH || y < 0 || y >= SCREEN_HEIGHT)
return;
volatile short int *pixel_addr =
(volatile short int *)(pixel_buffer_start + (y << 10) + (x << 1));
*pixel_addr = color;
}
void draw_char(int x, int y, char c, short color)
{
if (c < '0' || c > '9')
return;
int digit = c - '0';
for (int row = 0; row < 7; row++)
{
unsigned char row_data = font_digits[digit][row];
for (int col = 0; col < 5; col++)
{
if (row_data & (1 << (4 - col)))
plot_pixel(x + col, y + row, color);
}
}
}
void draw_string(int x, int y, const char *str, short color)
{
while (*str)
{
draw_char(x, y, *str, color);
x += 6;
str++;
}
}