13
Using State Machines as Control Circuits Lecture L9.4

Using State Machines as Control Circuits Lecture L9.4

  • View
    218

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Using State Machines as Control Circuits Lecture L9.4

Using State Machinesas Control Circuits

Lecture L9.4

Page 2: Using State Machines as Control Circuits Lecture L9.4

Using State Machines as Control Circuits

• Designing a Game: Simon-nomiS

• Implementing Simon-nomiS using ABEL

Page 3: Using State Machines as Control Circuits Lecture L9.4

Simon-nomiS

Game Outputs Player Inputs

Red Red

Red Blue Blue Red

Red Blue Green Green Blue Red

Red Blue Green Yellow Yellow Green Blue Red

WINNER

1

2

3

4

Simon-nomiS Player

Page 4: Using State Machines as Control Circuits Lecture L9.4

Simon-nomiS

IterationCounter, C

WINNER

ShowRed

ShowGreen

LOSTGAME

ShowYellow

ShowBlue

WONGAME

UserInput1

UserInput3

UserInput4

UserInput2

C = 0/C := 1

C <> 0

C <> 1

C <> 2

I <> 1000

I <> 0010

I <> 0001

I <> 0100

I = 1000 &C <> 4

I = 0010

I = 0001

I = 0100

I = 1000 &C = 4

C := 4

C = 2/C := 3

C = 1/C := 2

ClrC := 0

Page 5: Using State Machines as Control Circuits Lecture L9.4

Simon-nomiS

R Y B GInput

R Y B GOutput

Page 6: Using State Machines as Control Circuits Lecture L9.4

SI

MON-NOMIS

Page 7: Using State Machines as Control Circuits Lecture L9.4

Using State Machines as Control Circuits

• Designing a Game: Simon-nomiS

• Implementing Simon-nomiS using ABEL

Page 8: Using State Machines as Control Circuits Lecture L9.4

MODULE SimonnomiSTITLE 'Simon-nomiS - A twisted game of the traditional Simon'

DECLARATIONS

" INPUT PINS "PB PIN 74; " push-button switch SW1 (clock)clear PIN 70; " push-button switch SW2

[R,Y,B,G] PIN 4,3,2,1; "Switches S7:1-4I = [R,Y,B,G];

" OUTPUT PINS "O3..O0 PIN 40,41,43,44 ISTYPE 'com'; " LED 13..16O = [O3..O0]; " 4-bit output vector

Q3..Q0 NODE istype 'reg';Q = [Q3..Q0];

C2..C0 NODE istype 'reg';C = [C2..C0];

Simon.abl

Iterationcounter

Page 9: Using State Machines as Control Circuits Lecture L9.4

" Definitions

QSTATE = [Q3, Q2, Q1, Q0];

ShowRed = [0, 0, 0, 0];

ShowBlue = [0, 0, 0, 1];

ShowGreen = [0, 0, 1, 0];

ShowYellow = [0, 0, 1, 1];

UserInput1 = [0, 1, 0, 0];

UserInput2 = [0, 1, 0, 1];

UserInput3 = [0, 1, 1, 0];

UserInput4 = [0, 1, 1, 1];

LostGame = [1, 0, 0, 0];

WonGame = [1, 0, 0, 1];

Simon.abl (cont.)

Page 10: Using State Machines as Control Circuits Lecture L9.4

Simon.abl (cont.)state_diagram QSTATE

state ShowRed:

if C == 0 then UserInput1 with C := 1;

else ShowBlue with C := C;

state ShowBlue:

if C == 1 then UserInput2 with C := 2;

else ShowGreen with C := C;

state ShowGreen:

if C == 2 then UserInput3 with C := 3;

else ShowYellow with C := C;

state ShowYellow:

goto UserInput4;

WINNER

ShowRed

ShowGreen

LOSTGAME

ShowYellow

ShowBlue

WONGAME

UserInput1

UserInput3

UserInput4

UserInput2

C = 0/C := 1

C <> 0

C <> 1

C <> 2

I <> 1000

I <> 0010

I <> 0001

I <> 0100

I = 1000 &C <> 4

I = 0010

I = 0001

I = 0100

I = 1000 &C = 4

C := 4

C = 2/C := 3

C = 1/C := 2

ClrC := 0

Page 11: Using State Machines as Control Circuits Lecture L9.4

state UserInput1: if (I == 8) & (C == 4) then WonGame with C := 0 else if I == 8 then ShowRed with C := C; else LostGame; state UserInput2: if I == 2 then UserInput1 with C := C else LostGame; state UserInput3: if I == 1 then UserInput2 with C := C else LostGame; state UserInput4: if I == 4 then UserInput3 with C := C else LostGame; state WonGame: goto WonGame; state LostGame: goto LostGame;

Simon.abl (cont.)

WINNER

ShowRed

ShowGreen

LOSTGAME

ShowYellow

ShowBlue

WONGAME

UserInput1

UserInput3

UserInput4

UserInput2

C = 0/C := 1

C <> 0

C <> 1

C <> 2

I <> 1000

I <> 0010

I <> 0001

I <> 0100

I = 1000 &C <> 4

I = 0010

I = 0001

I = 0100

I = 1000 &C = 4

C := 4

C = 2/C := 3

C = 1/C := 2

ClrC := 0

Page 12: Using State Machines as Control Circuits Lecture L9.4

EQUATIONS

@radix 16;

WHEN QSTATE == ShowRed then O = 8;

ELSE WHEN QSTATE == ShowBlue then O = 2;

ELSE WHEN QSTATE == ShowGreen then O = 1;

ELSE WHEN QSTATE == ShowYellow then {O = 4; C := 4;}

ELSE WHEN QSTATE == WonGame then {O = 0F; C := 0;}

ELSE WHEN QSTATE == LostGame then {O = 9; C := C;}

ELSE O = 0;

Q.C = PB;

Q.AR = clear;

C.C = PB;

C.AR = clear;

Remember – O is combinational – C is registered

Simon.abl (cont.)

Output Ois a functionof the state

Page 13: Using State Machines as Control Circuits Lecture L9.4

test_vectors([PB,clear,I] -> [Q,C,O])[.c.,1,.X.] -> [0,0,8];[.c.,0,.X.] -> [4,1,0];[.c.,0,8] -> [0,1,8];[.c.,0,.X.] -> [1,1,2];[.c.,0,.X.] -> [5,2,0];[.c.,0,2] -> [4,2,0];[.c.,0,8] -> [0,2,8];[.c.,0,.X.] -> [1,2,2];[.c.,0,.X.] -> [2,2,1];[.c.,0,.X.] -> [6,3,0];[.c.,0,1] -> [5,3,0];[.c.,0,2] -> [4,3,0];[.c.,0,8] -> [0,3,8];[.c.,0,.X.] -> [1,3,2];[.c.,0,.X.] -> [2,3,1];[.c.,0,.X.] -> [3,3,4];[.c.,0,.X.] -> [7,4,0];[.c.,0,4] -> [6,4,0];[.c.,0,1] -> [5,4,0];[.c.,0,2] -> [4,4,0];[.c.,0,8] -> [9,0,0F]; "--winning[.c.,0,.X.] -> [9,0,0F]; "--winning"[.c.,0,4] -> [8,0,9]; "[.c.,0,.X.] -> [8,0,9]; " -- replace the last two with the two" -- above for Losing a game

END SimonnomiS

Simon.abl (cont.)