46
Toward a general purpose computer Example: Game of Life

Toward a general purpose computer Example: Game of Life

Embed Size (px)

Citation preview

Page 1: Toward a general purpose computer Example: Game of Life

Toward a general purpose computer

Example: Game of Life

Page 2: Toward a general purpose computer Example: Game of Life

Game of Life

For Each Line

For Each Cell Solve Cell

Page 3: Toward a general purpose computer Example: Game of Life

Game of Life

For Each Line

For Each Cell Solve Cell

Generation Line CellMatrix

Page 4: Toward a general purpose computer Example: Game of Life

Modularity

Generation Module Matrix Module Line Module Cell Module

Page 5: Toward a general purpose computer Example: Game of Life

Algorithm for the Generation Module

1. CurrentGen = 02. Solve for current Matrix3. CurrentGen++4. If CurrentGen < MAX_GEN

goto 2

Page 6: Toward a general purpose computer Example: Game of Life

Solve Matrix

0. j = 11. Calculate line (j)2. If j < MAX_LINE

2.1 j++2.2 goto 1.

3. STOP

Page 7: Toward a general purpose computer Example: Game of Life

Calculate Line j0. i = 0, tmp = 01. Calculate Cell i.2. If alive, set location i in tmp to alive3. If i < MAX_CELL

3.1 i++3.2 goto 1.

4. Store tmp in : even generation: i+16odd generation : i-16

Page 8: Toward a general purpose computer Example: Game of Life

CellAlive(cell index i, line index j)

0. N = Count_Neighbors(i,j-1,false)1. N = N+Count_Neighbors(i,j,true)2. N =

N+Count_Neighbors(i,j+1,false)3. If N > MIN && N < MAX 3.1 Return alive4. Return dead

Page 9: Toward a general purpose computer Example: Game of Life

Count_Neighbors(index i,line j,

center)

0. N = 0 1. If alive in location [i-1,j]: N=N+12. If not center:

2.1 If alive in location [i,j]: N=N+1

3. If alive in location [i+1,j]: N=N+14. Return N

Page 10: Toward a general purpose computer Example: Game of Life

Algorithm implementation

Algorithm

How to do? What to do?

The operation itself control

Page 11: Toward a general purpose computer Example: Game of Life

The operations in the algorithm:

Operations: Add Subtract

Page 12: Toward a general purpose computer Example: Game of Life

The operation:Defining interfaces

CountNeighbors

CellAlive

Calculate Line

Solve Matrix

Generation

Return Number of Neighbor: N

Alive: 0..010..0

Dead: 0000000

(i-1) location

Return the line

The matrix

Return

The Operation itself

Page 13: Toward a general purpose computer Example: Game of Life

Implementing instructionsModule Count_Neighbors, instruction 1, 2.1, 3

decode(i-1) = 0001000AND

Line (j) = 00010100

Result = 00010000

decode(i-1) = 0001000AND

Line (j) = 00000100

Result = 00000000

Zero – cell in location i is dead

Non Zero – cell in location i is alive

If alive in location [i-1,j]

The Operation itself

Page 14: Toward a general purpose computer Example: Game of Life

Implementing instructionsModule Count_Neighbors, instruction 1, 2.1, 3

Alive if AND(line,decode(i-1)) is not zero

If alive in location [i-1,j]

The Operation itself

Page 15: Toward a general purpose computer Example: Game of Life

The operations in the algorithm:

Operations: Add Subtract The logic AND + Check if zero

The Operation itself

Page 16: Toward a general purpose computer Example: Game of Life

Small ALU (arithmetic logic unit)

Op selector

Meaning Result

00 Add Res = InputA+InputB

01 Subract Res = InputA+ NOT(InputB)+1

10 AND Res = AND(InputA,InputB)

11 Decode Res = decode(InputA)

The Operation itself

Page 17: Toward a general purpose computer Example: Game of Life

Small ALU

MUX

S0S1S2

Adder

Adder

1

ReductionOR

Zero Result

Input A Input B

Decode

StatusBit

The Operation itself

Page 18: Toward a general purpose computer Example: Game of Life

Small ALU

StatusBit

The Operation itself

ALU

isZero

Op

InputBInputA

Page 19: Toward a general purpose computer Example: Game of Life

Implementation of AND(line,decode(i+1))

Reg1

Reg2

Reg3

Regn

Page 20: Toward a general purpose computer Example: Game of Life

Implementation of AND(line,decode(i+1))

Reg1

Reg2

Reg3

Regn

ReadA

ReadB

1

2

Connected like MUX 1

Page 21: Toward a general purpose computer Example: Game of Life

Implementation of AND(line,decode(i+1))

Reg1

Reg2

Reg3

Regn

ReadA

ReadB

1

2

Decoder

Enable Write

Write Address

Write Data

Page 22: Toward a general purpose computer Example: Game of Life

Implementation of AND(line,decode(i+1))

Registers

Data 1

Data 2

Data 1 Address

Data 2 Address

WriteAddress

Write Data

Write

Page 23: Toward a general purpose computer Example: Game of Life

Variables of the algorithmCount_Neighbors

Variable Meaning AddressCurrentLine Hold the current

line0

I The current index 1

N The Neighbor 2

Tmp Temporary space 3

Constant 1 Constant 1 5

ControlVariable A flag to determine if the operation should

be performed

6

Center Is it the center 7

Page 24: Toward a general purpose computer Example: Game of Life

Variables of the algorithmCount_Neighbors

Variable Meaning AddressCurrentLine Hold the current

line0

I The current index 1

N The Neighbor 2

Tmp Temporary space 3

Constant 1 Constant 1 5

ControlVariable A flag to determine if the operation should

be performed

6

Center Is it the center 7

Page 25: Toward a general purpose computer Example: Game of Life

Register/ALU circuit

Registers

Data 1 Address

Data 2 Address

WriteAddress

Write

ALU

Op

Extend 1to 16

MU

X R0R1

ExternalInput

0

1

2

Page 26: Toward a general purpose computer Example: Game of Life

Control Algorithm is zero[AND(line,decode(i+1))]

If alive in location [i+1,j]: N=N+1

Algorithm instruction

Instruction a. Tmp = is zero[AND(Currentline,decode(i+1))]

b. If controlVariable=0000001: N=N+1

The Operation itself

Page 27: Toward a general purpose computer Example: Game of Life

Control Algorithm is zero[AND(line,decode(i-1))]

Tmp = is zero[AND(line,decode(i-1))]Instruction

The Operation itself

t0. Tmp = i-1t1. Tmp = decode(Tmp)t2. controlVariable = is zero[AND(CurrentLine, Tmp)]

Micro-Instruction

Page 28: Toward a general purpose computer Example: Game of Life

Micro instruction implementation

t0. Data 1 Address = 1Data 2 Address = 5WriteAddress = 3

Write = 1 Op = 01R0R1 = 01

Micro-Instruction

The Operation itself

a. Tmp = i+1b. Tmp = decode(Tmp)c. controlVariable = is zero[AND(CurrentLine, Tmp)]

Subtract

i1

tmp

The result of the ALU

Page 29: Toward a general purpose computer Example: Game of Life

Micro instruction implementation

t1. Data 1 Address = 3Data 2 Address = 0WriteAddress = 4

Write = 1 Op = 11R0R1 = 01

The Operation itself

a. Tmp = i+1b. Tmp = decode(Tmp)c. controlVariable = is zero[AND(CurrentLine, Tmp)]

Decode

tmpIrrelevant

tmp

The result of the ALU

Micro-Instruction

Page 30: Toward a general purpose computer Example: Game of Life

Micro instruction implementation

t2. Data 1 Address = 0Data 2 Address = 3WriteAddress = 6

Write = 1 Op = 10R0R1 = 00

The Operation itself

a. Tmp = i+1b. Tmp = decode(Tmp)c. controlVariable = is zero[AND(CurrentLine, Tmp)]

And

Line in register itmpcontrolVariable

The zero status bit

Micro-Instruction

Page 31: Toward a general purpose computer Example: Game of Life

Instruction Hierarchy

Instruction

Micro-Instruction

Micro-Instruction

Micro-Instruction

Micro-Instruction

Instruction

Algorithmic Instruction

The Operation itself

Page 32: Toward a general purpose computer Example: Game of Life

Instruction Hierarchy

Instruction

t0: Micro-Instruction

tn: Micro-Instruction

t0: Micro-Instruction

tn: Micro-Instruction

Instruction

Each micro instruction = 1 cycle

Timing Variable

s

Page 33: Toward a general purpose computer Example: Game of Life

Timing variables (example with 4 time variables)

CP

t0

t1

t2

t3

One instruction

Page 34: Toward a general purpose computer Example: Game of Life

Generating Timing variablesexample with 16 timing variables

CP Register 4bits

Adder

1Decoder

t0

t15

Page 35: Toward a general purpose computer Example: Game of Life

Control of Count_Neighbor

1. N=0

3. i-1 alive?

4. N=N+1

5. Is Center?

6. i alive

7. N=N+1

Yes

No

No

Yes

8. i+1 alive

Yes

No

9. N=N+1

end

Yes

No

Page 36: Toward a general purpose computer Example: Game of Life

Control of Count_Neighbor

0000

0001

0010

0011

0100

0101

0110

0111

1000

1. N=0

3. i-1 alive?

4. N=N+1

5. Is Center?

6. i alive

7. N=N+1

Yes

No

No

Yes

8. i+1 alive

Yes

No

9. N=N+1

end

Yes

No

Page 37: Toward a general purpose computer Example: Game of Life

Control of Count_Neighbor

0000

0001

0010

0011

0100

0101

0110

0111

1000

1. N=0

3. i-1 alive?

4. N=N+1

5. Is Center?

6. i alive

7. N=N+1

Yes

No

No

Yes

8. i+1 alive

Yes

No

9. N=N+1

end

Yes

No

Page 38: Toward a general purpose computer Example: Game of Life

Putting it all together:implementing what to do with ROM

Current State

Timing CV C D1 D2 WA W Op R0R1t0 t1 t2 t3

ROM Input ROM Output

The current state(4 bits) The current timing variable

(1 bits)

Control variable (16bits)

CenterRegister 7 (16bits)

Page 39: Toward a general purpose computer Example: Game of Life

Putting it all together: implementing what to do with ROM

Data 1 Address

ROM Input ROM Output

Data 2 Address Write-Data MUX control

WriteAddress

WriteALU Operation

Current State

Timing CV C D1 D2 WA

W Op R0R1

t0 t1 t2 t3

Page 40: Toward a general purpose computer Example: Game of Life

Putting it all together: implementing what to do with ROMROM Input ROM Output

Current State

Timing CV C D1 D2 WA W Op R0R1

t0 t1 t2 t3

0001 1 0 0 0 1 5 3 1 01 01

0001 0 1 0 0 3 0 4 1 11 01

0001 0 0 1 0 0 3 6 1 10 00

0010 0 0 0 1 0 0 0 0 000 01t0. Tmp = i+1t1. Tmp = decode(Tmp)t2. controlVariable = is

zero[AND(CurrentLine, Tmp)]t3. do nothing

Page 41: Toward a general purpose computer Example: Game of Life

Putting it all together: implementing what to do with ROMROM Input ROM Output

Current State

Timing CV C D1 D2 WA W Op R0R1

t0 t1 t2 t3

0001 1 0 0 0 1 5 3 1 00 01

0001 0 1 0 0 3 0 4 1 11 01

0001 0 0 1 0 0 3 6 1 10 00

0010 0 0 0 1 0 0 0 0 000 01t0. Tmp = i+1t1. Tmp = decode(Tmp)t2. controlVariable = is

zero[AND(CurrentLine, Tmp)]t3. do nothing

Page 42: Toward a general purpose computer Example: Game of Life

Putting it all together: implementing what to do with ROMROM Input ROM Output

Current State

Timing CV C D1 D2 WA W Op R0R1

t0 t1 t2 t3

0001 1 0 0 0 1 5 3 1 00 01

0001 0 1 0 0 3 0 4 1 11 01

0001 0 0 1 0 0 3 6 1 10 00

0010 0 0 0 1 0 0 0 0 000 01t0. Tmp = i+1t1. Tmp = decode(Tmp)t2. controlVariable = is

zero[AND(CurrentLine, Tmp)]t3. do nothing

Page 43: Toward a general purpose computer Example: Game of Life

Putting it all together: implementing what to do with ROMROM Input ROM Output

Current State

Timing CV C D1 D2 WA W Op R0R1

t0 t1 t2 t3

0001 1 0 0 0 1 5 3 1 00 01

0001 0 1 0 0 3 0 4 1 11 01

0001 0 0 1 0 0 3 6 1 10 00

0010 0 0 0 1 0 0 0 0 000 01t0. Tmp = i+1t1. Tmp = decode(Tmp)t2. controlVariable = is

zero[AND(CurrentLine, Tmp)]t3. do nothing

Page 44: Toward a general purpose computer Example: Game of Life

Control of Count_Neighbor

0000

0001

0010

0011

0100

0101

0110

0111

1000

1. N=0

3. i-1 alive?

4. N=N+1

5. Is Center?

6. i alive

7. N=N+1

Yes

No

No

Yes

8. i+1 alive

Yes

No

9. N=N+1

end

Yes

No

t0. Set Current State 0001t1. Set Current State 0001t2. Set Current State 0001t3. If ControlVariable=1

Set Current State 0010t3. If ControlVariable=0

Set Current State 0011

Current State = 0001

Page 45: Toward a general purpose computer Example: Game of Life

Putting it all together:implmenting control with ROM

ROM Input ROM Output

Current State

Timing CV C NextState

t0 t1 t2 t3

0001 1 0 0 0 0001

0001 0 1 0 0 0001

0001 0 0 1 0 0001

0001 0 0 0 1 1 0010

0001 0 0 0 1 0 0011

Here is the branch.

Page 46: Toward a general purpose computer Example: Game of Life

ROM

Timing Variable CurrentState

Registers

ALUExtend 1

to 16

MU

XR0R1

Resultzerobit

Op

D1D2

WA

Write

WriteData

Next State

Current State