/* Complicated Moore FSM filename fsm.c Simulator program of a Moore Finite State Machine This example accompanies the book "Embedded Microcomputer Systems: Real Time Interfacing", Brooks-Cole, copyright (c) 2000, Jonathan W. Valvano 7/6/99 Interface between the TExaS simulator running a MC68HC11 EVB with ICC11 freeware compiler TExaS Copyright 1999 by Jonathan W. Valvano for more information about ICC11 see http://www.imagecraft.com You may use this file without restrictions */ /* PortC bits 7-0 are inputs, Port B bits 7-0 are outputs */ #include "HC11.h" /* Each state can have up to 4 next state arrows The input to cause a state change is a sequence of 8 1,0,X E.g., 1X0XX1XX means Bit7=1, Bit5=0 and Bit2=1 This pattern is encoded with AndMask=0xA4 and EquMask=0x84 */ struct State{ unsigned char Out; /* Output to Port B */ unsigned int Wait; /* Time (E cycles) to wait in this state */ unsigned char AndMask[4]; unsigned char EquMask[4]; struct State *Next[4];}; /* Next state if input=matches */ typedef struct State StateType; typedef StateType * StatePtr; #define SA &fsm[0] #define SB &fsm[1] #define SC &fsm[2] StateType fsm[3]={ {0x34, 200, /* 100us */ {0xFF, 0xF0, 0x27, 0x00}, {0x51, 0xA0, 0x07, 0x00}, {SB,SA,SB,SC}}, {0xB3,80, /* 40 us */ {0x80, 0xF0, 0x00, 0x00}, {0x00, 0x90, 0x00, 0x00}, {SC,SA,SB,SB}}, {0x76,100, /* 50us */ {0xFF, 0x0F, 0x01, 0x00}, {0x12, 0x05, 0x00, 0x00}, {SA,SA,SB,SA}} }; StatePtr Pt; /* pointer to current state */ unsigned char Input; /* input from machine */ void Wait(unsigned int delay){ int Endt; Endt=TCNT+delay; /* Time (500ns cycles) to wait */ while((Endt-(int)TCNT)>0); /* wait */ }; void main(void){ unsigned int i; Pt=SA; /* Initial State */ DDRC=0x00; /* PortC are inputs, PortB is output */ while(1){ PORTB=Pt->Out; Wait(Pt->Wait); /* Time to wait in this state */ Input=PORTC; /* Input=0 to 255 */ for(i=0;i<4;i++) if((Input&(Pt->AndMask[i]))==(Pt->EquMask[i])){ Pt=Pt->Next[i]; i=4; } }}; void resetVectors(void){ /* not really a function, but this must be last */ asm(" org $FFFE"); asm(" FDB __start"); asm(" org $FFd5"); /* puts rts in a harmless place */ }