CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.

31
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics

Transcript of CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.

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

VideoController

Display

SoundController

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

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0F 0E 0D 0C 0B 0A 09 08 07 06 05 04 03 02 01 00

ModeBG0BG1BG2BG3

• Bits 0-2 Mode (at 0X4 000 000)– 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

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0F 0E 0D 0C 0B 0A 09 08 07 06 05 04 03 02 01 00

ModeBG0BG1BG2BG3

• Bits 0-2 Mode – 0,1,2 Tile Modes– 3, 4, 5 Bitmap Modes

• For bitmapped graphics use BG2• 100000000112 = 210 + 21 + 20 = 1024 + 2 + 1 = 1027

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

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;

}

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 <a valid c-type> <alias>

• 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...

C Operator Precedence and Associativity Operator Description Associativity

() [] . ->

++ --

Parentheses (grouping) Brackets (array subscript) Member selection via object name Member selection via pointer Postfix increment/decrement (see Note 1)

left-to-right

++ -- + - ! ~

(type) * &

sizeof

Prefix increment/decrement Unary plus/minus Logical negation/bitwise complement Cast (change type) Dereference Address Determine size in bytes

right-to-left

* / % Multiplication/division/modulus left-to-right

+ - Addition/subtraction left-to-right

<< >> Bitwise shift left, Bitwise shift right left-to-right

< <= > >=

Relational less than/less than or equal to Relational greater than/greater than or equal to

left-to-right

== != Relational is equal to/is not equal to left-to-right

& Bitwise AND left-to-right

^ Bitwise exclusive OR left-to-right

| Bitwise inclusive OR left-to-right

&& Logical AND left-to-right

|| Logical OR left-to-right

?: Ternary conditional right-to-left

= += -= *= /= %= &= ^= |=

<<= >>=

Assignment Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment

right-to-left

, Comma (separate expressions) left-to-right

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:– 7 6 5 4 3 2 1 0

• To set bit 3 of x where x is an 8 bit quantityx = x | (1 << 3)

• To set bit n of x where x is an 8 bit quantityx = 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:– 7 6 5 4 3 2 1 0

• To clear bit 3 of x where x is an 8 bit quantityx = x & ~(1 << 3)

• To clear bit n of x where x is an 8 bit quantityx = 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:– 7 6 5 4 3 2 1 0

• To test bit 3 of x where x is an 8 bit quantityif( x & (1 << 3) )

• To set bit n of x where x is an 8 bit quantityif( 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?