Slides

advertisement
Practical session 8
Assignment 3
Game of life
• A zero-player game.
• Simulates Evolution, of an
infinite two-dimensional
matrix’s cells.
• Each cell can be alive or
dead.
• A cell’s state in each
iteration (generation) is set
with accordance to its
former state and its
neighbors’ states.
Our version
• We’ll simulate a simplified, one-dimensional
(finite) array.
• Each cell is given an initial state on program
startup
• At each “generation”, a cell will determine its
next state according to its former state and its
neighbors’ states, using a given transition
function
Implementation
• Using the co-routine mechanism, we’ll write a
small simulator for the game.
• Running co-routines:
– n cell instances
– A printer
– A scheduler
A Cell
• Can be alive(1) or dead(0)
• Executes a simple infinite loop:
1. Calculate next state using current state (of “me”
and my left and right neighbors).
2. Update new state
• After each of these two stages, the cell coroutine must resume the scheduler.
In other words, it loops (for ever) over:
(1) resume  (2) resume
A Cell
• Just to be perfectly clear:
– celli‘s left neighbor is celli-1 .
– celli‘s right neighbor is celli+1 .
– The Array “wraps around” 
calculations are %WorldSize
A Cell
• How do we determine a cell’s next step?
– One of the program’s parameters is a transition
function:
• A sequence of hexadecimal digits (a nibble of bits) such
as “B1E3”.
• Each digit represents a transition.
B  1011
new state
Left neighbor’s
state
“My” state
Right neighbor’s
state
Transition example
• Given this transition function:
B=1011
1=0001
E=1110
3=0011
• Say a cell is dead, its left neighbor is dead and its right
neighbor is alive, then “3” can be applied, and thus
the next state is alive (1).
• When no transition can be applied, no transition is
done:
• Say a cell is alive, its left neighbor is alive and its right
neighbor is dead  no transition fits the case (we’d
need either “C” or “D”), and thus no change is made.
The printer
• Simply prints the entire “world” (the array),
whenever it gets “time”.
The scheduler
• You are to implement a simple round-robin
scheduler.
• void scheduler(int cycles)
– Iterate cycles times over all cells
– After each K resumes of cell co-routines, resume the
printer.
– At the end of all cycles, resume the printer once
more, and then terminate the process.
Program’s flow
• Invoked using:
>ass3 <states> <t> <K> <transition_func>
• where:
– <states> - A series of “0” and “1”. describes the
initial state, as well as the amount of cells
– <t> - Amount of scheduler cycles through each cell
– <K> - Printing frequency
– <transition_func> - A series of hexadecimal digits
Program’s flow
• When invoked, and using the co-routines
mechanism from class, your application will:
– Set up: a state array, WorldSize , K
– Initiate all co-routines:
• Each cell needs to get a parameter i with its index.
• The scheduler needs to get cycles (t) as a parameter.
• Initiate the printer
– Initiate an array CORS of pointers to:
cell1, cell2, …, cellWorldSize, scheduler, printer.
– Transfer control to scheduler
Technicalities
• You are to submit a single .zip file, using the
same directory structure as in previous
assignments.
• src folder will include:
– ass3.s (with most of the code)
– printer.s (printer function code)
– scheduler.s (scheduler function code)
Example
• Let’s run an example using your simple
scheduler:
>ass3 01010010 5 7 029B46DF
• First 8 “time slices”
produce readings
and state
computations:
Cell
left
curr next
0
0
0
1
2
0
1
0
1
0
0
2
1
0
1
4
B
3
0
1
0
4
0
4
1
0
0
9
1
5
0
0
1
2
0
6
0
1
0
4
0
7
1
0
0
9
1
New
1
Example
01010010
00101010
00101001
10011001
10010100
10010100
;after 7 “resumes”. No updates.
;after 14; 6 updates made.
;(21): no second updates yet
;(28): 4 second cycle updates
;(35): 2nd cycle updated
;(40): last print (done cycling)
Download