Saturday, December 9, 2023

Machine language monitor with BASIC, check!

 Pointless, but I had fun, for sure!


What follows is VERY SLIGHTLY self-modifying BASIC code. There's a save routine on rows 2-5, and a bug on row 7, I should remove that. Anyway, the filename after sys command HAS TO be a static string. But.... As long as I change NOTHING on rows 1-2, it is always in a fixed position. So, on row 640 I POKE a little fun out of it. 

The default is "ALANGPROGRAM" because it started out as "MLANGPROGRAM" but I had to check which offset was the correct one by poking at it, so now it's an "A". 

Self-modifying code is pretty cool and legal. 

1 goto10
2 sys57812"alangprogram",8
3 poke193,al-(ha*256):poke194,ha
4 poke174,lo-(hl*256):poke175,hl
5 sys62954
6 print"reset now"
7 poke43,0:poke44,8:poke45,189:poke46,12
10 rem memory monitor and editor
30 i=1:m=0:p=0:s=0
35 print"@<address>,<q>uit":print"return
:dump 8"
40 print str$(s);:input a$
44 if a$="" then goto 400
45 if mid$(a$,i,1)=" " then i=i+1
46 goto 300
50 gosub 150:i=i+1
60 d=b*16
70 gosub 150
80 b=d+b
90 print str$(b)+" ";
95 if p=8 then print "":p=0
96 p=p+1
100 poke s+m, b:m=m+1
110 if i=len(a$) then goto 200
120 i=i+1
140 goto 45
150 rem get octal as decimal
151 b=asc(mid$(a$,i,1))
160 if b<64 then b=b-48
170 if b>64 then b=b-55
180 return
200 s=s+m: print""
205 a$=""
210 i=1:m=0:p=1:goto40
220 rem end
300 rem command handlers
310 if a$="q" goto 1000
320 if left$(a$,1)="@" then goto 500
330 if a$="s" then goto 600
390 goto 50
400 rem print 8 byte in hex
401 print chr$(145) + str$(s) +": ";
402 for j=0 to 7: rb=peek(s+j)
410 hi=((240 and rb)/16)+48:lo=(rb and 1
5)+48
420 if hi>57 then hi=hi+7
421 if lo>57 then lo=lo+7
430 print chr$(hi)+chr$(lo)+" ";
440 next j
441 s=s+8:m=0:p=1:i=1:j=0:b=0:d=0
442 print ""
450 goto 40
499 rem memory jump command (@)
500 s=val(mid$(a$,2,len(a$)-1)):i=1:m=0:
p=0
505 a$=""
510 goto 40
600 rem save perhaps?
610 input "start memory address:";al
611 input "end memory address:";lo
612 input "filename:";fi$
620 if fi$="" goto 695
635 ha=int(al/256):hl=int(lo/256)
639 fi$=left$(fi$,12)
640 fori=1tolen(fi$):c=asc(mid$(fi$,i,1)
):poke2048+19+i,c:nexti
690 goto 2
695 i=1:m=0:a$="":goto 40
1000 rem end
So, what's left?
Minifying and optimizing this, maybe. In retrospect, you should write your basic WIDE initially, maybe use rem statements to note sections and subroutines. It would make refactoring - including minifying and optimizing - easier.
It would be handy  to be able to switch to character and decimal modes, and making use of the default screen editor. 
I should probs also write a sprite/tile editor one of these days, but basic IS kinda tedious in some ways. I could write a macroassembler, yes. But on Commodore? In BASIC? unlikely. 
But I do want to do that game of some sort. Maybe a roguelike or action roguelike? I also have some further stuff to do on a "text editor". I've had some sort-of working ones, but I can improve them, and they are useful for some stuff... they're also just a skip and a hop away from being a level editor so that's also useful. 

I'm also doing some other stuff right now, ended up writing a sort of an experimental macroparser thingy, with the help of a LLM  today.


Wednesday, October 25, 2023

This was a while back. Minidium from C=1/87

 I typed in a listing, successfully, and I sorta understood what was going on. 

Here's a link to the magazine it's from: https://archive.org/details/C-Lehti-1987-1/mode/2up

Again, trauma therapy. Never successfully wrote a working program from a listing as a child; Certainly not one with a bunch of machine language hex dump in it.