EE 319 K Introduction to Embedded Systems 2
EE 319 K Introduction to Embedded Systems 2 -D Arrays, Bitmaps, Sprites, Game Engines 13 -1
Agenda q Agenda v. Software design v 2 -D array v. Bitmaps v. Wav Files v. Game Engines v. Lab 9 9 -2
Software Design q Modular programming v. Make it easier to understand o Each screen is a complete story without scrolling v. Maximize the number of modules v. Minimize the interdependency o Bandwidth of data passed from one to another o Control coupling: actions in one cause effects in another o Shared variables (very bad) Book Section 5. 2 9 -3
Software Design q Design for test v. Consider how it will be tested while designing v. Module has three files o Header: What the module does o Code: How it works o Test: A main program used to test the module q Manage resources v. LCD graphics v. Time (processor cycles) o A fun game requires careful control of time v. Input/Output o Switches, slide pot, DAC, LCD 9 -4
2 -D Array or Matrix q What: 2 rows and 3 columns, 8 bits each vunsigned char M[2][3]; q Why: v. Images v. Maps q How: (C uses row major) v. C code to access M[i][j] = 5; v. Write this in assembly (R 0=i, R 1=j) i = row j = column n= # of columns Base+n*i+j Base+2*(n*i+j) Base+4*(n*i+j) Num of bytes/element 9 -5
2 -D Array or Matrix q What: 6 rows and 7 columns vshort Connect 4[6][7]; q Why: v. Images v. Maps j i Base+2*(7*i+j) q How: (row major) v. Write C code to set array values to 0 9 -6
2 -D Array or Matrix q Assuming C[6][7] // check the rows for(i=0; i<6; i++){ for(j=0; j<4; j++){ if((C[i][j]==1) &&(C[i][j+1]==1) &&(C[i][j+2]==1) &&(C[i][j+3]==1)){ Iwin(); } } } j i 0 means free 1 means me -1 means you 9 -7
Nokia 5110 Graphics Format Column 0 Row 48 LCD is 48 rows, 84 columns, 1 bits/pixel Column 84 9 -8
BMP File Format q Sprites as objects moving across screen Sprites are monochrome W pixels wide by H pixels high 9 -9
BMP File Format Nokia 5110_Print. BMP(48, 24, Small. Enemy 10 point. A, 0); 16 wide, 10 high F Placed at x=48, y=24 F FFFFFFFFFF FFF FFFF F F F F The raw data from BMP file to illustrate how the image is stored (0 s replaced with spaces). 9 -10
BMP File Format const unsigned char Enemy 10 Point 1[] = { 0 x 42, 0 x 4 D, 0 x. C 6, 0 x 00, 0 x 00, 0 x 76, 0 x 00, 0 x 28, 0 x 00, 0 x 10, 0 x 00, 0 x 0 A, 0 x 00, 0 x 01, 0 x 00, 0 x 04, 0 x 00, 0 x 00, 0 x 50, 0 x 00, 0 x 00, 0 x 00, 0 x 00 , 0 x 00, 0 x 00, 0 x 00, 0 x 80, 0 x 00, 0 x 80 , 0 x 00, 0 x 80, 0 x 00, 0 x 00, 0 x 80, 0 x 00, 0 x 80, 0 x 80, 0 x 00, 0 x. C 0, 0 x 00, 0 x. FF, 0 x 00, 0 x. FF , 0 x 00, 0 x 00, 0 x. FF, 0 x 00, 0 x. FF , 0 x 00, 0 x. FF, 0 x 00, 0 x 00, 0 x 00, 0 x 0 F, 0 x 00, 0 x 00, 0 x. F 0, 0 x 00, 0 x 0 F, 0 x 00, 0 x 0 F, 0 x. FF, 0 x. F 0, 0 x 00, 0 x. FF, 0 x 00, 0 x. FF, 0 x. F 0, 0 x. FF, 0 x 0 F, 0 x. FF, 0 x 00, 0 x. FF, 0 x 0 F, 0 x 00, 0 x 00, 0 x. F 0, 0 x 00, 0 x 0 F, 0 x 00, 0 x 00, 0 x 00, 9 -11 0 x. FF Example BMP file written as C constant q Header (w x h) q Pixel data We use 4 -bit BMP but all values above a threshold are deemed 1 for monochrome.
BMP Header Format 9 -12
Timer 2 A Periodic interrupt • Resolution: bus period • Precision: 32 bits • Max period: 53 sec (80 MHz) 0) activate timer 2 clock 1) disable timer 2 A 2) Precision to 32 bits 3) periodic mode 4) TAILR value 5) clock resolution 6) clear timeout flag 7) arm timeout 8) priority 4 9) enable in NVIC 10) enable timer 2 A 9 -13
INTERRUPT VECTORS 77 total Vector address 0 x 00000038 0 x 0000003 C 0 x 00000040 0 x 00000044 0 x 00000048 0 x 0000004 C 0 x 00000050 0 x 00000054 0 x 00000058 0 x 0000005 C 0 x 00000060 0 x 00000064 0 x 00000068 0 x 0000006 C 0 x 00000070 0 x 00000074 0 x 00000078 0 x 0000007 C 0 x 00000080 0 x 00000084 0 x 00000088 0 x 0000008 C 0 x 00000090 0 x 00000094 0 x 00000098 0 x 0000009 C 0 x 000000 A 0 0 x 000000 A 4 0 x 000000 A 8 0 x 000000 AC 0 x 000000 B 0 0 x 000000 B 4 0 x 000000 B 8 0 x 000000 BC 0 x 000000 C 0 0 x 000000 C 4 0 x 000000 C 8 0 x 000000 CC 0 x 000000 D 0 0 x 000000 D 4 0 x 000000 D 8 0 x 000000 DC 0 x 000000 E 0 0 x 000000 E 4 0 x 000000 E 8 0 x 000000 EC 0 x 000000 F 0 0 x 000000 F 4 0 x 000000 F 8 0 x 000000 FC Number 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 IRQ -2 -1 0 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 ISR name in Startup. s Pend. SV_Handler Sys. Tick_Handler GPIOPort. A_Handler GPIOPort. B_Handler GPIOPort. C_Handler GPIOPort. D_Handler GPIOPort. E_Handler UART 0_Handler UART 1_Handler SSI 0_Handler I 2 C 0_Handler PWMFault_Handler PWM 0_Handler PWM 1_Handler PWM 2_Handler Quadrature 0_Handler ADC 1_Handler ADC 2_Handler ADC 3_Handler WDT_Handler Timer 0 A_Handler Timer 0 B_Handler Timer 1 A_Handler Timer 1 B_Handler Timer 2 A_Handler Timer 2 B_Handler Comp 0_Handler Comp 1_Handler Comp 2_Handler Sys. Ctl_Handler Flash. Ctl_Handler GPIOPort. F_Handler GPIOPort. G_Handler GPIOPort. H_Handler UART 2_Handler SSI 1_Handler Timer 3 A_Handler Timer 3 B_Handler I 2 C 1_Handler Quadrature 1_Handler CAN 0_Handler CAN 1_Handler CAN 2_Handler Ethernet_Handler Hibernate_Handler USB 0_Handler PWM 3_Handler u. DMA_Error Lab 7 Lab 8 Lab 9 NVIC_SYS_PRI 3_R NVIC_PRI 0_R NVIC_PRI 1_R NVIC_PRI 2_R NVIC_PRI 3_R NVIC_PRI 4_R NVIC_PRI 5_R NVIC_PRI 6_R NVIC_PRI 7_R NVIC_PRI 8_R NVIC_PRI 9_R NVIC_PRI 10_R NVIC_PRI 11_R NVIC_PRI 11_R Priority bits 23 – 21 31 – 29 7– 5 15 – 13 23 – 21 31 – 29 7– 5 15 – 13 23 – 21 31 – 29 7– 5 15 – 13 23 – 21 31 – 29 9 -14
Timer 2 A Periodic interrupt unsigned long Timer. Count; Max is 53 sec void Timer 2_Init(unsigned long period){ unsigned long volatile delay; SYSCTL_RCGCTIMER_R |= 0 x 04; // 0) activate timer 2 delay = SYSCTL_RCGCTIMER_R; Timer. Count = 0; TIMER 2_CTL_R = 0 x 0000; // 1) disable timer 2 A TIMER 2_CFG_R = 0 x 0000; // 2) 32 -bit mode TIMER 2_TAMR_R = 0 x 00000002; // 3) periodic mode TIMER 2_TAILR_R = period-1; // 4) reload value TIMER 2_TAPR_R = 0; // 5) clock resolution TIMER 2_ICR_R = 0 x 00000001; // 6) clear timeout flag TIMER 2_IMR_R = 0 x 00000001; // 7) arm timeout NVIC_PRI 5_R = (NVIC_PRI 5_R&0 x 00 FFFFFF)|0 x 80000000; // 8) priority 4 NVIC_EN 0_R = 1<<23; // 9) enable IRQ 23 in TIMER 2_CTL_R = 0 x 00000001; // 10) enable timer 2 A } 9 -15 Output sound at 11. 025 k. Hz
Timer 2 A plays sounds // trigger is Timer 2 A Time-Out Interrupt // set periodically TATORIS set on rollover void Timer 2 A_Handler(void){ TIMER 2_ICR_R = 0 x 00000001; // acknowledge Timer. Count++; // run some background stuff here } TATORIS Output sounds here void Timer 2 A_Stop(void){ TIMER 2_CTL_R &= ~0 x 00000001; // disable } Ack Stuff Call to stop sound void Timer 2 A_Start(void){ TIMER 2_CTL_R |= 0 x 00000001; } // enable Call to start sound 9 -16
Lab 9 – Space Invaders, Pipe Dreams … q There must be at least one button and one slide pot. q There must be at least three images on the LCD display that move. q There must be sounds appropriate for the game. q The score should be displayed on the screen (but it could be displayed before or after the game action). q At least two interrupt ISRs must used in an appropriate manner. q The game must have a “time” aspect to it (For e. g. , if you don’t move a sprite within a certain time it could be killed). Contrast with Connect. Four which is a taking “turns” game q The game must be both simple to learn and fun to play. 9 -17
Lab 9 – Grading q The TA will certify your game and put you into one of two groups. Group A or B q Lab 9 will be graded subjectively by other students v During class we will setup two groups of stations each v One team member demonstrates v The other team member scores other 7 games in group q Games are rank-ordered according to level of fun. Score is based on rank in group. v 80 if 0 th to 49 th percentile v 90 if 50 th to 74 th percentile v 100 if 75 th to 100 th percentile 9 -18
- Slides: 18