10
20
30
40 :
50 ON ERROR REPORT:PROCClose_All:PRINT:PROCexit(ERR):END
60 max%=256:DIM ctrl% 31,mem% max%,type$(3):X%=ctrl%:Y%=X%DIV256:in%=0:rd%=4:data%=mem%+1
70 A$=FNOS_GetEnv:IFos%>31:PROCWin_TextIO
80 IFA$="":INPUT"Input TAP file: "A$
90 A%=INSTR(A$+" "," "):IFA%:in$=LEFT$(A$,A%-1):A$=MID$(A$,A%+1)
100 IFin$="-?":PRINT"Syntax: TAPDump <filename>":PROCexit(0):END
110 in%=FNf_openin(in$):IFin%=0:PRINT"'"in$"' not found":PROCexit(214):END
120 IFin$="":PROCexit(0):END
130 :
140 type$(0)="Program":type$(2)="Number":type$(3)="Character":type$(3)="Code"
150 PROCgbpb(rd%,in%,mem%,10,0):mem%?7=13
160 tap%=(($mem%="ZXTape!"AND4))OR(((!mem%AND&FFFFFF)=&11)AND1)
170 IF(tap%AND4)=0:PTR#in%=0
180 :
190 REPEAT
200 IF(tap%AND4):REPEATUNTILBGET#in%=&10:A%=BGET#in%+BGET#in%
210 len%=BGET#in%+256*BGET#in%+(tap%AND1)
220 IFlen%>max% :PROCgbpb(rd%,in%,mem%,max%,0):PTR#in%=PTR#in%+len%-max%
230 IFlen%<=max%:IFlen%:PROCgbpb(rd%,in%,mem%,len%,0)
240 IF?mem%=0 :PROCheader
250 IF?mem%=&FF:PROCdata
260 UNTIL EOF#in%
270 CLOSE#in%:in%=0:PROCexit(0):END
280 :
290 DEFPROCclose:IFin%:A%=in%:CLOSE#A%
300 ENDPROC
310 :
320 DEFPROCheader
330 IFmem%?1<4:PRINT type$(mem%?1);": "; ELSE PRINT "Type ";mem%?1;": ";
340 PRINT TAB(9);
350 m%=mem%?12:mem%?12=13:f$=$(mem%+2):mem%?12=m%:num%=mem%!12 AND &FFFF
360 start%=mem%!14 AND &FFFF:exec%=mem%!16 AND &FFFF
370 PRINT FNfn_clean(f$)" "FNh0(start%,4)"+"FNh0(num%,4)" "FNh0(exec%,4)
380 ENDPROC
390 :
400 DEFPROCdata
410 ptr%=PTR#in%-len%+1
420 FOR a%=0 TO len%-3 STEP 16
430 b%=a% AND (max%-1):PRINT FNh0(a%,4)": ";
440 IFb%=max%-16:PROCgbpb(rd%-1,in%,data%+255,1,ptr%+a%+15)
450 FOR c%=b% TO b%+15:IFa%+(c%AND15)<len%-2+(tap%AND1):PRINT FNh0(data%?c%,2);" "; ELSE PRINTSPC3;:data%?c%=32
460 NEXT:FOR c%=b% TO b%+15:IFdata%?c%=127 OR data%?c%<32:VDU46 ELSE VDU data%?c%
470 NEXT:IFb%=max%-16:PROCgbpb(rd%-1,in%,data%,255,ptr%+a%+16)
480 PRINT:NEXT:PTR#in%=ptr%+len%-1
490 ENDPROC
500 :
510 DEFFNfn_clean(A$):IF A$="":=""
520 FOR A%=1 TO LEN A$:IF MID$(A$,A%,1)<" " OR MID$(A$,A%,1)>"~":A$=LEFT$(A$,A%-1)+"_"+MID$(A$,A%+1)
530 NEXT:=A$
540 :
550 DEFFNh0(A%,N%)=RIGHT$("0000000"+STR$~A%,N%)
560 :
570 DEFPROCClose_All:*EXEC
580 in%=in%:IFin%:A%=in%:in%=0:CLOSE#A%
590 ENDPROC
600 :
610 DEFFNf_openin(A$)=OPENIN(FNf_name(A$))
620 DEFFNf_name(A$):IFos%>31:LOCALA%,B%:REPEATB%=A%:A%=INSTR(A$,"\",A%+1):UNTILA%=0:IFINSTR(A$,".",B%)=0:A$=A$+"."
630 =A$
640 :
650 DEFPROCgbpb(A%,chn%,addr%,num%,ptr%)
660 ?X%=chn%:X%!1=addr%:X%!5=num%:X%!9=ptr%:IFPAGE<&FFFFF:CALL &FFD1:ENDPROC
670 IFA%=1ORA%=3:PTR#?X%=X%!9
680 REPEAT:IFA%=1ORA%=2:BPUT#?X%,?X%!1 ELSE IFA%=3ORA%=4:?X%!1=BGET#?X%
690 X%!1=X%!1+1:X%!5=X%!5-1:UNTIL(EOF#?X% AND A%>2)OR X%!5<1:ENDPROC
700 :
710 DEFFNOS_GetEnv:LOCALA%,X%,Y%,P%,A$:X%=1:os%=((USR&FFF4)AND&FF00)DIV256:DIMX%TRUE
720 IF!PAGE=&D7C1C7C5:run$=ARGV$(0):IFARGC:FORA%=1TOARGC:A$=A$+ARGV$(A%)+" ":NEXT:=LEFT$(A$,LENA$-1)ELSEIF!PAGE=&D7C1C7C5:=""
730 IFPAGE>&FFFFF:DIMX%LOCAL256:A$=@cmd$:SYS"GetModuleFileName",0,X%,255:run$=$$X%:Y%=INSTR(@lib$,@tmp$)=0:P%=TRUE
740 IFP%=0:IFHIMEM>&FFFF:run$=$&8100:SYS"OS_GetEnv"TOA$,,A%:SYS"OS_WriteEnv","",A%:A$=MID$(A$,1+INSTR(A$+" "," ",1+INSTR(A$," "))):P%=TRUE:Y%=TRUE:IFINSTR(A$," ")=0:A$=run$+" "
750 IFP%=0:P%=X%:[OPT 0:NOP:]:P%=?X%:Y%=TRUE:IFP%=&EAORP%=18:A$=$&600 ELSEIFP%=0:A$=$(PAGE-&300)ELSEIFP%=&A0:A$=$(^@%-256):Y%=?(PAGE-2)AND64ELSEIFP%=&90:A$=$&100:Y%=!&200
760 A$=" "+A$:REPEATREPEATA$=MID$(A$,2):UNTILASCA$<>32:IFASCA$=34:A%=INSTR(A$,"""",2)+1ELSEA%=INSTR(A$+" "," ")
770 IFY%:run$=LEFT$(A$,A%-1):A$=MID$(A$,A%+1):Y%=0
780 UNTILASCA$<>32:=A$
790 :
800 DEFPROCos(A$):IFASCA$=42:OSCLIA$ ELSE IFA$<>"":CHAINA$
810 ENDPROC
820 :
830 DEFPROCexit(A%):OSCLI"FX1,"+STR$(A%AND255):quit$=quit$:A$=quit$:quit$="":PROCos(A$)
840 IFPAGE>&FFFFF:QUIT A% ELSE END
850 ENDPROC
860 :
870 DEFPROCWin_TextIO
880 SYS "GetStdHandle",-10 TO @hfile%(1):*INPUT 13
890 SYS "GetStdHandle",-11 TO @hfile%(2):*OUTPUT 14
900 SYS "SetConsoleMode",@hfile%(1),0:ENDPROC
910 :