Skip to content

Instantly share code, notes, and snippets.

@mnemnion
Created April 16, 2014 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnemnion/10912594 to your computer and use it in GitHub Desktop.
Save mnemnion/10912594 to your computer and use it in GitHub Desktop.
A simple hex dump for Retro (11)
( Simple Hex Dump )
needs bad'
needs console'
8 constant (dump-width)
: putxt xt->d d->name puts ;
: hexabyte ( "hexes a byte" )
dup 10 <
[ 48 + ]
[ 87 + ] if ;
: byte>cha ( byte -> < := cha true | byte false > -- := ?cha )
( "filters out non-printable ascii" )
dup 32 126 within ;
: putbyte dup 4 >> hexabyte putc
16 mod hexabyte putc
;
: putcell ( cell -> nil )
^bad'unpack 4 [ putbyte ] times ;
: normalText ( - ) ^console'console? [ "\[0m" puts ] ifFalse ;
: (highlight) ( cell -> cell )
( "highlight cells " )
dup xt->d 0 <>
[ ^console'RED ^console'foreground ]
[ ^console'normalText ] if
;
"" variable: dump-str
: dot-or-cha
dump-str @ swap
byte>cha
[ ^strings'appendChar ]
[ drop '. ^strings'appendChar ] if
dump-str !
;
: (stringulate) ( cell -> cell )
dup ^bad'unpack 4 [ dot-or-cha ] times
;
: dotpr
dump-str @
puts
"" dump-str !
;
: smart-dump ( adr range -> nil "hexdump")
[
(dump-width) mod 0 =
[ ^console'normalText
10 [ space ] times
dotpr
cr dup putcell ': putc space space ] ifTrue
dup @ (highlight) (stringulate) putcell space 1+ ] iter
^console'normalText 10 [ space ] times dotpr
;
: dump
[
(dump-width) mod 0 =
[ 10 [ space ] times
dotpr
cr dup putcell ': putc space space ] ifTrue
dup @ (stringulate) putcell space 1+ ] iter
10 [ space ] times
dotpr
;
: dump-me 12 23 putcell ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment