From 4f66956b93358d943df0610015e02865d1c3339d Mon Sep 17 00:00:00 2001 From: James Date: Fri, 11 Oct 2013 10:34:23 +0100 Subject: now with scores --- pong3.pin | 4 +- pong3.qsf | 2 + software/pong3/pong3.c | 167 +++++++++++++++++++++++++++++++++++----- software/pong3_bsp/settings.bsp | 4 +- 4 files changed, 152 insertions(+), 25 deletions(-) diff --git a/pong3.pin b/pong3.pin index 85a0886..cc630e7 100644 --- a/pong3.pin +++ b/pong3.pin @@ -104,7 +104,7 @@ seven_seg[5] : 37 : output : 3.3-V LVTTL : GND : 38 : gnd : : : : seven_seg[2] : 39 : output : 3.3-V LVTTL : : 1 : Y seven_seg[6] : 40 : output : 3.3-V LVTTL : : 1 : Y -GND* : 41 : : : : 1 : +seven_seg[7] : 41 : output : 3.3-V LVTTL : : 1 : Y VCCIO1 : 42 : power : : 3.3V : 1 : seven_seg[4] : 43 : output : 3.3-V LVTTL : : 1 : Y seven_seg[3] : 44 : output : 3.3-V LVTTL : : 1 : Y @@ -243,7 +243,7 @@ GND* : 176 : : : GND : 177 : gnd : : : : VCCINT : 178 : power : : 1.2V : : GND* : 179 : : : : 2 : -seven_seg[7] : 180 : output : 3.3-V LVTTL : : 2 : N +GND* : 180 : : : : 2 : GND* : 181 : : : : 2 : GND* : 182 : : : : 2 : VCCIO2 : 183 : power : : 3.3V : 2 : diff --git a/pong3.qsf b/pong3.qsf index f8ef54f..b614ba7 100644 --- a/pong3.qsf +++ b/pong3.qsf @@ -48,6 +48,7 @@ set_global_assignment -name DEVICE_FILTER_PACKAGE "ANY QFP" set_global_assignment -name DEVICE_FILTER_PIN_COUNT 208 set_global_assignment -name DEVICE_FILTER_SPEED_GRADE 8 set_global_assignment -name ERROR_CHECK_FREQUENCY_DIVISOR 1 +set_location_assignment PIN_41 -to seven_seg[7] set_location_assignment PIN_40 -to seven_seg[6] set_location_assignment PIN_37 -to seven_seg[5] set_location_assignment PIN_43 -to seven_seg[4] @@ -149,4 +150,5 @@ set_global_assignment -name VERILOG_FILE pong3.v set_global_assignment -name QSYS_FILE my_sys.qsys + set_instance_assignment -name PARTITION_HIERARCHY root_partition -to | -section_id Top \ No newline at end of file diff --git a/software/pong3/pong3.c b/software/pong3/pong3.c index 39ee99c..09240ff 100644 --- a/software/pong3/pong3.c +++ b/software/pong3/pong3.c @@ -10,18 +10,47 @@ #define msleep(msec) usleep(1000*msec); - + +static void +pio_write (unsigned int data) +{ + IOWR (PIO_0_BASE, 0, data); +} + + static void gpu_write (unsigned int reg, unsigned int data) { IOWR (GPU_0_BASE, reg << 2, data); } +static void +show_score (int score) +{ + const uint8_t lookup[10] = + { 0x3F, 0x6, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x7, 0x7F, 0x6F }; + uint8_t ss = 0; + + if (score < 0) + { + ss |= 0x80; + score = -score; + } + if (score > 9) + score = 9; + ss |= lookup[score]; + + pio_write (ss); +} + static void load_sprite (void) { - const uint16_t sprite[16]={ 0x00C0, 0x03E0, 0x0FF8, 0x1FFE, 0x3FC7, 0x3F83, 0x3933, 0x3987, 0x3D37, 0x3D37, 0x3D87, 0x3FFF, 0x1FFE, 0x07F8, 0x01E0, 0x00C0 }; - const unsigned int n=sizeof(sprite)/sizeof(uint16_t); + const uint16_t sprite[16] = + { 0x00C0, 0x03E0, 0x0FF8, 0x1FFE, 0x3FC7, 0x3F83, 0x3933, 0x3987, 0x3D37, + 0x3D37, 0x3D87, 0x3FFF, 0x1FFE, 0x07F8, 0x01E0, 0x00C0 + }; + const unsigned int n = sizeof (sprite) / sizeof (uint16_t); unsigned int i; // RRR GGG BBB @@ -29,8 +58,8 @@ load_sprite (void) gpu_write (GPU_REG_SPRITE_COLOUR, 0x01D0); // squirt the bromium logo into the sprite - for (i=0;i 20) + return 1; + return 0; +} + +static int +x_speed (void) +{ + return (rand () % 10) + 1; +} + +static int +y_speed (void) +{ + return (rand () % 9) - 4; +} + + int main (void) { int x, y, xd, yd; int bat0, dbat0; int bat1, dbat1; - int missed = 0; + int score = 0; + int serve = 0; + int i; printf ("Working...\n"); msleep (500); + +#if 0 + while (1) + { + for (i = -10; i < 10; ++i) + { + show_score (i); + msleep (500); + } + + } +#endif + srand (12392184); - bat0 = 100; - bat1 = 200; + bat0 = GPU_HEIGHT / 2; + bat1 = GPU_HEIGHT / 2; load_sprite (); - while (1) + for (;;) { - x = 1; - y = 1; - xd = 1; - yd = 1; + xd = x_speed (); + yd = y_speed (); - dbat0 = find_intersection (x + xd, y + yd, xd, yd, 0); - dbat1 = find_intersection (x + xd, y + yd, xd, yd, GPU_WIDTH-1); + show_score (score); - while (!missed) + if (!serve) + { + x = 1; + y = bat0; + dbat0 = GPU_HEIGHT / 2; + dbat1 = find_intersection (x + xd, y + yd, xd, yd, GPU_WIDTH - 1); + } + else + { + x = GPU_WIDTH - 2; + y = bat1; + yd = -yd; + dbat0 = find_intersection (x + xd, y + yd, xd, yd, 0); + dbat1 = GPU_HEIGHT / 2; + } + serve = !serve; + + for (;;) { x += xd; y += yd; @@ -125,22 +207,35 @@ main (void) if (squish (&x, 0, GPU_WIDTH)) { - xd = (rand () % 3) + 1; + xd = x_speed (); if (x) xd = -xd; - yd = rand () % 7; - yd -= 3; + yd = y_speed (); if (x) { + + if (has_missed (y, bat1)) + { + score++; + break; + } dbat0 = find_intersection (x + xd, y + yd, xd, yd, 0); dbat1 = GPU_HEIGHT / 2; + } else { + + if (has_missed (y, bat0)) + { + score--; + break; + } dbat0 = GPU_HEIGHT / 2; + dbat1 = + find_intersection (x + xd, y + yd, xd, yd, GPU_WIDTH - 1); - dbat1 = find_intersection (x + xd, y + yd, xd, yd, GPU_WIDTH-1); } } @@ -157,6 +252,36 @@ main (void) msleep (2); } + + if (!serve) + { + dbat1 = GPU_HEIGHT / 2; + dbat0 = bat0; + } + else + { + dbat0 = GPU_HEIGHT / 2; + dbat1 = bat1; + } + + for (i = 0; i < 200; ++i) + { + if (i == 100) + show_score (score); + + move_bat (&bat0, dbat0); + move_bat (&bat1, dbat1); + + + gpu_write (1, x + GPU_OFFSET); + gpu_write (2, y + GPU_OFFSET); + gpu_write (3, bat0 + GPU_OFFSET); + gpu_write (4, bat1 + GPU_OFFSET); + + gpu_write (0, 0); + + msleep (2); + + } } } - diff --git a/software/pong3_bsp/settings.bsp b/software/pong3_bsp/settings.bsp index acec6f1..335d873 100644 --- a/software/pong3_bsp/settings.bsp +++ b/software/pong3_bsp/settings.bsp @@ -2,8 +2,8 @@ hal default - Oct 11, 2013 9:35:07 AM - 1381480507860 + Oct 11, 2013 10:05:01 AM + 1381482301065 /home/root/projects/altera/pong3/software/pong3_bsp ./settings.bsp ../../my_sys.sopcinfo -- cgit v1.2.3