clearlabels

org ea5e

sketch_x:
  defb 0
sketch_y:
  defb 0

findvar:
  ld hl,(5c4b)
findvar_loop:
  ld a,(hl)
  ld bc,$19
  and e0
  cp e0
  jr z,findvar1
  cp 60
  jr z,findvar2
  cp a0
  jr nz,findvar3
findvar_bitloop:
  inc hl
  bit 7,(hl)
  jr z,findvar_bitloop
findvar2:
  ld c,6
  jr findvar1
findvar3:
  cp 80
  jr z,findvar4
  cp c0
  jr nz,findvar5
findvar4:
  ld a,(hl)
  cp 80
  jr z,findvar_end
  inc hl
  ld c,(hl)
  inc hl
  ld b,(hl)
  inc hl
  jr findvar1
findvar5:
  cp 40
  jr nz,findvar6
  ld a,(hl)
  cp 43
  inc hl
  ld c,(hl)
  inc hl
  ld b,(hl)
  inc hl
  jr z,sketch
  jr findvar1
findvar6:
  inc hl
findvar1:
  add hl,bc
  jr findvar_loop
findvar_end:
  ld (len_c),bc
  ld (var_c),hl
  ret

sketch:
  ld a,b
  or c
  ret z
  ld d,h
  ld e,l
  ld hl,(sketch_x)
  ld (tmp_sketchx),hl
  ld hl,sketch_one
  ld a,(hl)
  add a,'0'
  ld ixl,a
  ld a,1
  sub (hl)
  add a,'0'
  ld ixh,a
  ld a,(iy+$87)
  ld (mem_pflag),a
  xor a
  ld (loopcount),a
  ld a,(tmp_sketchx)
  ld h,a
  ld l,a
sketch_loop:
sketch_test0:
  ld a,(de)
  cp ixh
  jr nz,sketch_test1
  set 3,(iy+$87)
sketch_plot:
  push hl
  push de
  push bc
  ld a,l
  ld c,a
  ld a,(tmp_sketchy)
  ld b,a
  call plotxy
  pop bc
  pop de
  pop hl
sketch_move:
  inc l
  jp sketch_ready
sketch_test1:
  cp ixl
  jr nz,sketch_teststar
  res 3,(iy+$87)
  jr sketch_plot
sketch_teststar:
  cp '*'
  jr z,sketch_move
sketch_testE:
  cp 'e'
  jr nz,sketch_testW
  inc h
  jr sketch_move
sketch_testW:
  cp 'w'
  jr nz,sketch_testN
  dec h
  dec l
  jp sketch_ready
sketch_testN:
  cp 'n'
  jr nz,sketch_testS
  push hl
  ld hl,tmp_sketchy
  inc (hl)
  pop hl
  ld a,h
  ld l,a
  jp sketch_ready
sketch_testS:
  cp 's'
  jr nz,sketch_testI
  push hl
  ld hl,tmp_sketchy
  dec (hl)
  pop hl
  ld a,h
  ld l,a
  jp sketch_ready
sketch_testI:
  cp 'i'
  jr nz,sketch_testL
  ld a,'1'
  sub ixh
  add a,'0'
  ld ixh,a
  ld a,'1'
  sub ixl
  add a,'0'
  ld ixl,a
  jp sketch_ready
sketch_testL:
  cp ':'
  jr nz,sketch_testBracket
  inc de
  dec bc
  ld a,b
  or c
  jp z,sketch_end
sketch_evalnumber:
  xor a
  ld (sketch_number),a
sketch_evalnumloop:
  ld a,(de)
  cp '0'
  jr c,sketch_evalnumber1
  cp ':'
  jr nc,sketch_evalnumber1
  sub '0'
  push bc
  ld c,a
  ld a,(sketch_number)
  ld b,a
  add a,a
  add a,a
  add a,b
  add a,a
  add a,c
  pop bc
  ld (sketch_number),a
  jr sketch_evalnumbercont
sketch_evalnumber1:
  cp '('
  jr nz,sketch_end
sketch_evalnumberend:
  inc de
  dec bc
  ld a,b
  or c
  jr z,sketch_end
  jr sketch_pushinstack
sketch_evalnumbercont:
  inc de
  dec bc
  ld a,b
  or c
  jr z,sketch_end
  jr sketch_evalnumloop    
sketch_pushinstack:
  ld a,(loopcount)
  cp 5
  jr nc,sketch_end
  inc a
  ld (loopcount),a
  push hl
  call getstackindex
  ld a,(sketch_number)
  ld (hl),a
  inc hl
  ld (hl),c
  inc hl
  ld (hl),b
  inc hl
  ld (hl),e
  inc hl
  ld (hl),d
  pop hl
  jp sketch_loop
sketch_testBracket:
  cp ')'
  jr nz,sketch_testQ
  ld a,(loopcount)
  or a
  jr z,sketch_end
  push hl
  call getstackindex
  ld a,(hl)
  or a
  jr z,sketch_loopend
  dec (hl)
  inc hl
  ld c,(hl)
  inc hl
  ld b,(hl)
  inc hl
  ld e,(hl)
  inc hl
  ld d,(hl)
  pop hl
  jp sketch_loop
sketch_loopend:
  ld hl,loopcount
  dec (hl)
  pop hl
  jr sketch_ready
sketch_testQ:
  cp 'q'
  jr z,sketch_end
sketch_ready:
  inc de
  dec bc
  ld a,b
  or c
  jp nz,sketch_loop  
sketch_end:
  ld a,(mem_pflag)
  and $15
  ld e,a
  ld a,(iy+$87)
  and $240
  or e
  ld (iy+$87),a
  ret

getstackindex:
  push bc
  ld hl,loopstack
  dec a
  ld c,a
  add a,a
  add a,a
  add a,c
  ld c,a
  ld b,0
  add hl,bc
  pop bc
  ret

tmp_sketchx:
  defb 0
tmp_sketchy:
  defb 0
var_c:
  defw 0
len_c:
  defw 0
mem_pflag:
  defb 0
sketch_one:
  defb 1
sketch_number:
  defb 0

plotxy:
  push bc
  call getyposadr
  pop bc
  ld a,c
  rra
  rra
  rra
  and $31
  add a,l
  ld l,a
  ld a,c
  and $7
  ld b,a
  or a
  jr z,plotxy_bit7
  ld a,$128
plotxy_bitloop:
  rra
  djnz plotxy_bitloop
  jr plotxy_bit0to6
plotxy_bit7:
  ld a,$128
plotxy_bit0to6:
  ld e,(iy+$87)
  bit 1,e
  jr nz,plotxy_over1
plotxy_over0:
  bit 3,e
  jr nz,plotxy_over0inv1
plotxy_over0inv0:
  or (hl)
  ld (hl),a
  ret
plotxy_over0inv1:
  cpl
  and (hl)
  ld (hl),a
  ret
plotxy_over1:
  bit 3,e
  ret nz
plotxy_over1inv0:
  xor (hl)
  ld (hl),a  
  ret

getyposadr:
  ld hl,0
  ld a,b
  cp $192
  ret nc
  ld c,a
  ld b,l
  ld hl,startaddresses
  add hl,bc
  add hl,bc
  ld a,(hl)
  inc hl
  ld h,(hl)
  ld l,a
  ret

loopcount:
  defb 0
loopstack:
  defb 0
  defw 0
  defw 0

  defb 0
  defw 0
  defw 0

  defb 0
  defw 0
  defw 0

  defb 0
  defw 0
  defw 0

  defb 0
  defw 0
  defw 0

startaddresses:
defw $22496
defw $22240
defw $21984
defw $21728
defw $21472
defw $21216
defw $20960
defw $20704
defw $22464
defw $22208
defw $21952
defw $21696
defw $21440
defw $21184
defw $20928
defw $20672
defw $22432
defw $22176
defw $21920
defw $21664
defw $21408
defw $21152
defw $20896
defw $20640
defw $22400
defw $22144
defw $21888
defw $21632
defw $21376
defw $21120
defw $20864
defw $20608
defw $22368
defw $22112
defw $21856
defw $21600
defw $21344
defw $21088
defw $20832
defw $20576
defw $22336
defw $22080
defw $21824
defw $21568
defw $21312
defw $21056
defw $20800
defw $20544
defw $22304
defw $22048
defw $21792
defw $21536
defw $21280
defw $21024
defw $20768
defw $20512
defw $22272
defw $22016
defw $21760
defw $21504
defw $21248
defw $20992
defw $20736
defw $20480
defw $20448
defw $20192
defw $19936
defw $19680
defw $19424
defw $19168
defw $18912
defw $18656
defw $20416
defw $20160
defw $19904
defw $19648
defw $19392
defw $19136
defw $18880
defw $18624
defw $20384
defw $20128
defw $19872
defw $19616
defw $19360
defw $19104
defw $18848
defw $18592
defw $20352
defw $20096
defw $19840
defw $19584
defw $19328
defw $19072
defw $18816
defw $18560
defw $20320
defw $20064
defw $19808
defw $19552
defw $19296
defw $19040
defw $18784
defw $18528
defw $20288
defw $20032
defw $19776
defw $19520
defw $19264
defw $19008
defw $18752
defw $18496
defw $20256
defw $20000
defw $19744
defw $19488
defw $19232
defw $18976
defw $18720
defw $18464
defw $20224
defw $19968
defw $19712
defw $19456
defw $19200
defw $18944
defw $18688
defw $18432
defw $18400
defw $18144
defw $17888
defw $17632
defw $17376
defw $17120
defw $16864
defw $16608
defw $18368
defw $18112
defw $17856
defw $17600
defw $17344
defw $17088
defw $16832
defw $16576
defw $18336
defw $18080
defw $17824
defw $17568
defw $17312
defw $17056
defw $16800
defw $16544
defw $18304
defw $18048
defw $17792
defw $17536
defw $17280
defw $17024
defw $16768
defw $16512
defw $18272
defw $18016
defw $17760
defw $17504
defw $17248
defw $16992
defw $16736
defw $16480
defw $18240
defw $17984
defw $17728
defw $17472
defw $17216
defw $16960
defw $16704
defw $16448
defw $18208
defw $17952
defw $17696
defw $17440
defw $17184
defw $16928
defw $16672
defw $16416
defw $18176
defw $17920
defw $17664
defw $17408
defw $17152
defw $16896
defw $16640
defw $16384
