/* Maze solver - using left hand rule */
/* Copyright J Marshall 1999 */
/*  Press reset lots of times and watch */
/* the little dot that starts in the right */
/* hand bottom corner of the CRT */


/*variables at 44 */
T44K
:templine:CONST(0,D) /* a temp value to store the current line */
:sparearg:CONST(0,D)
:xarg:CONST(1,F)
:yarg:CONST(1,F)
:returnvalue:CONST(1,F)
:loadcountx:CONST(34,F)
:loadcounty:CONST(15,F)
:sixteen:CONST(16,F)
:thirtyfive:CONST(34,F)
:thirtyfour:CONST(34,F)
:one:CONST(1,F)
:temp:CONST(1,F)
:four:CONST(4,F)
:returnadd:CONST(0,F)// value to add to do returns
:xpos:CONST(4,F)
:ypos:CONST(1,F)


:entry:
A3F
TreturnaddF

// Load maze
AloadcountyF	// load the y count
:loadloopy:
TloadcountyF	// store the y count

TtemplineD // zero the current line

AloadcountxF // load the x count
:loadloopx:
  TloadcountxF // store the x count

  AtemplineD
  L0D // shift that by one
  TtemplineD

  ItempF // input a 0 or a 1
  AtempF // load the input
  R128F // shift it right 9
  R128F // shift it right 9
  AtemplineD // merge it with the current line
  TtemplineD
  AloadcountxF
  SoneF
  EloadloopxF // the x loop

TloadcountxF
AthirtyfiveF
TloadcountxF
AtemplineD

:loadstore:
T0D

AloadstoreF
AfourF
TloadstoreF

AloadcountyF
SoneF
EloadloopyF







/* Now we have loaded the maze */


/* ok here is the actual solver
what it does is just start off going left and
continue on until there is 
a) a gap to the left, turn left
b) a blockage in front, turn right
or it reaches 33,14 (top left hand corner)
*/


:west:
// set this point
TtemplineD // zero accumulator
AxposF
TxargF
AyposF
TyargF
AoneF
R128F
R128F
TspareargD

:setwest:
AsetwestF
GsetpointF

ZF

// clear this point
TspareargD
SoneF
R128F
R128F
TspareargD
:unsetwest:
AunsetwestF
GsetpointF




// check the point to the left
TyargF
AyposF
SoneF
UyargF
TyposF 
// I change ypos here so that I can go straight
//  through to the south procedure if nothing is 
//  there 
:westleftcheck:
AwestleftcheckF
GcheckpointF
AreturnvalueF
EsouthF // change direction

TyargF
AyposF
AoneF // reset ypos
UyargF
TyposF

// check ahead 
TxargF
AxposF
AoneF
UxargF
TxposF  // can just return to the start of this bit

:westaheadcheck:
AwestaheadcheckF
GcheckpointF
AreturnvalueF

EwestF // full speed ahead
// reset xpos
TxargF
AxposF
SoneF
UxargF
TxposF
EnorthF









:south:
// set this point
TtemplineD // zero accumulator
AxposF
TxargF
AyposF
TyargF
AoneF
R128F
R128F
TspareargD

:setsouth:
AsetsouthF
GsetpointF

ZF

// clear this point
TspareargD
SoneF
R128F
R128F
TspareargD
:unsetsouth:
AunsetsouthF
GsetpointF




// check the point to the left
TxargF
AxposF
SoneF
UxargF
TxposF 
// I change ypos here so that I can go straight
//  through to the east procedure if nothing is 
//  there 
:southleftcheck:
AsouthleftcheckF
GcheckpointF
AreturnvalueF
EeastF // change direction

TxargF
AxposF
AoneF // reset ypos
UxargF
TxposF

// check ahead 
TyargF
AyposF
SoneF
UyargF
TyposF  // can just return to the start of this bit

:southaheadcheck:
AsouthaheadcheckF
GcheckpointF
AreturnvalueF

EsouthF // full speed ahead
// reset ypos
TyargF
AyposF
AoneF
UyargF
TyposF

EwestF

:east:
// set this point
TtemplineD // zero accumulator
AxposF
TxargF
AyposF
TyargF
AoneF
R128F
R128F
TspareargD

:seteast:
AseteastF
GsetpointF

ZF

// clear this point
TspareargD
SoneF
R128F
R128F
TspareargD
:unseteast:
AunseteastF
GsetpointF




// check the point to the left
TyargF
AyposF
AoneF
UyargF
TyposF 
// I change ypos here so that I can go straight
//  through to the south procedure if nothing is 
//  there 
:eastleftcheck:
AeastleftcheckF
GcheckpointF
AreturnvalueF
EnorthF // change direction

TyargF
AyposF
SoneF // reset ypos
UyargF
TyposF

// check ahead 
TxargF
AxposF
SoneF
UxargF
TxposF  // can just return to the start of this bit

:eastaheadcheck:
AeastaheadcheckF
GcheckpointF
AreturnvalueF

EeastF // full speed ahead
// reset xpos
TxargF
AxposF
AoneF
UxargF
TxposF
EsouthF











:north:
// set this point
TtemplineD // zero accumulator
AxposF
TxargF
AyposF
TyargF
AoneF
R128F
R128F
TspareargD

:setnorth:
AsetnorthF
GsetpointF

ZF

// clear this point
TspareargD
SoneF
R128F
R128F
TspareargD
:unsetnorth:
AunsetnorthF
GsetpointF




// check the point to the left
TxargF
AxposF
AoneF
UxargF
TxposF 
// I change xpos here so that I can go straight
//  through to the east procedure if nothing is 
//  there 
:northleftcheck:
AnorthleftcheckF
GcheckpointF
AreturnvalueF
EwestF // change direction

TxargF
AxposF
SoneF // reset ypos
UxargF
TxposF

// check ahead 
TyargF
AyposF
AoneF
UyargF
TyposF  // can just return to the start of this bit

:northaheadcheck:
AnorthaheadcheckF
GcheckpointF
AreturnvalueF

EnorthF // full speed ahead
// reset ypos
TyargF
AyposF
SoneF
UyargF
TyposF

EeastF









ZF



:setpoint:
/* adds sparearg << xarg to [yarg*2] */
AreturnaddF
TsetendF
AyargF
L1F
AsetloadF
TsetloadF

:setload: // load the line
A0D
TtemplineD

/*restore the load inst */
AsetloadorigF
TsetloadF

AxargF
TloadcountxF
:setloop:
AloadcountxF
SoneF
GsetloopendF
TloadcountxF

AspareargD
L0D
TspareargD

EsetloopF

:setloopend:
TloadcountxF

AyargF
L1F
AsetstoreF
TsetstoreF


AtemplineD
AspareargD
:setstore:
T0D

AsetstoreorigF
TsetstoreF

:setend:
ZF
:setloadorig:
A0D
:setstoreorig:
T0D






/* Check if point (x,y) is set - where x=memory 0
 y=memory 1 */

:checkpoint:
AreturnaddF
TcheckendF

AyargF

L1F
AcheckloadF
TcheckloadF

:checkload: // load the line
A0D
TtemplineD

/*restore the load inst */
AcheckloadorigF
TcheckloadF

AthirtyfourF
SxargF
TloadcountxF
:checkloop:
AloadcountxF
SoneF
GcheckloopendF
TloadcountxF

AtemplineD
L0D
TtemplineD

EcheckloopF

:checkloopend:
TloadcountxF
AtemplineD
TreturnvalueF

// neg if set, pos if not set
:checkend:
PF
:checkloadorig:
A0D





EentryK
PF

11111111111111111111111111111111111
11111111111111111100000000000000001
11111111111111111111111111110111111
11111111110000000001111111110111111
11111111110111111100000011110011111
11111101110000000001111011100111111
11111101110111111111111011110111111
11111101000000000001111000000111111
11111101111101110111111111111111111
11111101111101110111111111111111111
11000000000000000001111111111111111
11111111111111111101111111111111111
11000000000000111101111111111111111
11111101111110000001111111111111111
10000001111111111011111111111111111
11111111111111111111111111111111111

