CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics
CS1372: HELPING TO PUT THE COMPUTING IN ECE Computer Numbers Binary Two's complement Hexadecimal
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
CS1372: HELPING TO PUT THE COMPUTING IN ECE Machine Model Processor Memory Video Controller Display Sound Controller Speakers/ Jacks Button Controller Buttons
CS1372: HELPING TO PUT THE COMPUTING IN ECE Light Up a Pixel
CS1372: HELPING TO PUT THE COMPUTING IN ECE REG_DISPCNT F0E0D0C0B0A Mode BG0BG1BG2BG3 Bits 0-2 Mode (at 0X ) –0,1,2 Tile Modes –3, 4, 5 Bitmap Modes For bitmapped graphics use BG2
CS1372: HELPING TO PUT THE COMPUTING IN ECE REG_DISPCNT F0E0D0C0B0A Mode BG0BG1BG2BG3 Bits 0-2 Mode –0,1,2 Tile Modes –3, 4, 5 Bitmap Modes For bitmapped graphics use BG = = = 1027
CS1372: HELPING TO PUT THE COMPUTING IN ECE Video Memory (in Mode 3) Located at 0x Consists of 240 x bit shorts
CS1372: HELPING TO PUT THE COMPUTING IN ECE Let's Light Up a Pixel int main(void) { *(unsigned short *)0x = 1027; *(unsigned short *)0x = 32767; while(1) { } return 0; }
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.
CS1372: HELPING TO PUT THE COMPUTING IN ECE C Datatypes integers –int –short –long –unsigned floating point –float –double characters –char
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
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;
CS1372: HELPING TO PUT THE COMPUTING IN ECE C Logical Operators && || !
CS1372: HELPING TO PUT THE COMPUTING IN ECE Bitwise Operators & | ~ ^ >> <<
CS1372: HELPING TO PUT THE COMPUTING IN ECE While we're at it...
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
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
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
CS1372: HELPING TO PUT THE COMPUTING IN ECE How would we set a bit? Assume bits are numbered like this: – 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 +
CS1372: HELPING TO PUT THE COMPUTING IN ECE How would we clear a bit? Assume bits are numbered like this: – 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 -
CS1372: HELPING TO PUT THE COMPUTING IN ECE How would we test a bit? Assume bits are numbered like this: – 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)
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);
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;
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
CS1372: HELPING TO PUT THE COMPUTING IN ECE Questions?