Download presentation
Presentation is loading. Please wait.
Published byAshlyn Curtis Modified over 9 years ago
1
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics
2
CS1372: HELPING TO PUT THE COMPUTING IN ECE Computer Numbers Binary Two's complement Hexadecimal
3
CS1372: HELPING TO PUT THE COMPUTING IN ECE What are the components? Processor -- Does math, logic, testing (=0) Memory -- Stores data AND program –Also is where program interfaces with I/O devices Input -- Gameboy has 10 pushbuttons Output –Video screen –Sound
4
CS1372: HELPING TO PUT THE COMPUTING IN ECE Machine Model Processor Memory Video Controller Display Sound Controller Speakers/ Jacks Button Controller Buttons
5
CS1372: HELPING TO PUT THE COMPUTING IN ECE Light Up a Pixel
6
CS1372: HELPING TO PUT THE COMPUTING IN ECE REG_DISPCNT 15141312111009080706050403020100 0F0E0D0C0B0A09080706050403020100 Mode BG0BG1BG2BG3 Bits 0-2 Mode (at 0X4 000 000) –0,1,2 Tile Modes –3, 4, 5 Bitmap Modes For bitmapped graphics use BG2
7
CS1372: HELPING TO PUT THE COMPUTING IN ECE REG_DISPCNT 15141312111009080706050403020100 0F0E0D0C0B0A09080706050403020100 Mode BG0BG1BG2BG3 Bits 0-2 Mode –0,1,2 Tile Modes –3, 4, 5 Bitmap Modes For bitmapped graphics use BG2 10000000011 2 = 2 10 + 2 1 + 2 0 = 1024 + 2 + 1 = 1027
8
CS1372: HELPING TO PUT THE COMPUTING IN ECE Video Memory (in Mode 3) Located at 0x6 000 000 Consists of 240 x 160 16-bit shorts
9
CS1372: HELPING TO PUT THE COMPUTING IN ECE Let's Light Up a Pixel int main(void) { *(unsigned short *)0x4000000 = 1027; *(unsigned short *)0x6000240 = 32767; while(1) { } return 0; }
10
CS1372: HELPING TO PUT THE COMPUTING IN ECE Structure of C Program C programs consist of one or more files Each file may contain 0 or more functions Executable code must appear in functions Other items such as definitions, variable declarations, typedefs, etc may appear outside of functions. Where a variable is declared will affect where it is in memory, its scope and lifetime. C is compiled with a somewhat odd preprocessing step.
11
CS1372: HELPING TO PUT THE COMPUTING IN ECE C Datatypes integers –int –short –long –unsigned floating point –float –double characters –char
12
CS1372: HELPING TO PUT THE COMPUTING IN ECE “Build your Own Language” C is such a primitive language that we frequently need to expand its vocabulary in order to make code more understandable: typedef creates new data types (affects the compiler) #define is a preprocessor directive – modifies your source code before compilation
13
CS1372: HELPING TO PUT THE COMPUTING IN ECE typedef Allows you to define your own aliases for types Syntax: typedef Example –unsigned short is a valid c-type –So we can create out own alias using typedef unsigned short u16;
14
CS1372: HELPING TO PUT THE COMPUTING IN ECE C Logical Operators && || !
15
CS1372: HELPING TO PUT THE COMPUTING IN ECE Bitwise Operators & | ~ ^ >> <<
16
CS1372: HELPING TO PUT THE COMPUTING IN ECE While we're at it...
18
CS1372: HELPING TO PUT THE COMPUTING IN ECE Memory: Bits, Bytes, Words Memory consists of individual bits which may have a value of 0 or 1 but The smallest quantity of bits we can access is 8. This is known as a byte Bytes have address that increment by 1 Other data items which consist of groups of bytes have addresses which increment depending on the number of bytes –e.g. Shorts are 2 bytes long so their addresses are multiples of 2
19
CS1372: HELPING TO PUT THE COMPUTING IN ECE Addresses Addresses are usually expressed as hexadecimal numbers There are gaps in the address space Some areas of memory may be accessed as bytes, shorts and ints while others may only be accessed as shorts and ints –8, 16, 32 –16, 32
20
CS1372: HELPING TO PUT THE COMPUTING IN ECE Bit Vectors Storing multiple values (as individual bits or groups of bits) is known as a bit vector Bit vectors are used to save space Bit vectors are used extensively in GBA programming especially with I/O The key to bit vectors is the bitwise operations
21
CS1372: HELPING TO PUT THE COMPUTING IN ECE How would we set a bit? Assume bits are numbered like this: –7 6 5 4 3 2 1 0 To set bit 3 of x where x is an 8 bit quantity x = x | (1 << 3) To set bit n of x where x is an 8 bit quantity x = x | (1 << n) Note: Use | and not +
22
CS1372: HELPING TO PUT THE COMPUTING IN ECE How would we clear a bit? Assume bits are numbered like this: –7 6 5 4 3 2 1 0 To clear bit 3 of x where x is an 8 bit quantity x = x & ~(1 << 3) To clear bit n of x where x is an 8 bit quantity x = x & ~(1 << n) Note: Use & and not -
23
CS1372: HELPING TO PUT THE COMPUTING IN ECE How would we test a bit? Assume bits are numbered like this: –7 6 5 4 3 2 1 0 To test bit 3 of x where x is an 8 bit quantity if( x & (1 << 3) ) To set bit n of x where x is an 8 bit quantity if( x & (1 << n) ) If the desired bit is set, the expression will evaluate to true (i.e. In C, not zero)
24
CS1372: HELPING TO PUT THE COMPUTING IN ECE How can we put two values in one variable???? Assume we have a 16 bit short int called x and an unsigned char called y. We wish to replace the left half of x (8 high order bits) with y leaving the right half (8 low order bits) intact x = (x & 0xFF) | (y<<8);
25
CS1372: HELPING TO PUT THE COMPUTING IN ECE How can we put two values in one variable???? Assume we have a 16 bit short int called x and an unsigned char called y. We wish to replace the right half of x (8 low order bits) with y leaving the left half (8 high order bits) intact x = (x & 0xFF00) | y;
26
CS1372: HELPING TO PUT THE COMPUTING IN ECE How can we put three values in one variable? Color values are often stored in the GBA as 3 (Red, Green and Blue) five bit values packed into one unsigned short. xBBBBBGGGGGRRRRR We can code: R | G<<5 | B<<10 OR B<<10 | G<<5 | R Or as a macro #define COLOR(R, G, B) (R)|(G)<<5|(B)<<10
27
CS1372: HELPING TO PUT THE COMPUTING IN ECE Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.