In this vignette, we are going to:
c64asm
package and the R-specific .rtext
directivec64asm
- Using the .rtext
directive
One of the reasons to write this compiler in R is that R-specific ways of introducing data into the program can be included.
In this example, the .rtext
directive is used to include
text in a program that is taken directly from an R variable containing a
string.
create_ascii_art()
function which creates a 25 x 40 character matrix as an ascii version of
the given image
im <- create_ascii_art(image = jpeg::readJPEG(system.file('img/Rlogo.jpg',package='jpeg')))
cat(apply(im, 1, paste,collapse=''), sep='\n')
#> .:--::
#> =+======+++++++*.
#> =================++++*-
#> .===-=++=--==+++*+++++==++*+
#> ===-*++::-++##%%%%%%###**++++*:
#> ==:*+=.:-*#%% *#**+*=
#> =--*+..:+%# **+*#
#> ==**= :-%@ ..:.:::::::::::== *+*:
#> =-=*= .*%. .:+::::::::::::=*=* +#
#> =.#=..-%% .:+:::-++++=--:::==+ +.
#> :==*- .*@ .:+:::#+----*==:::=% ++
#> :=+*: :## .:+:::#- =.:::-% -*
#> .==*- :*# .:+:::#- : -:::% --
#> +=*=.:=% .:+:::#: :.*:: =@ =
#> :+++-.:#* .:+:::-::::. %-: +% %
#> ++++::=# .:+:::-=====-.:+#@ =
#> =+++=:-+# .:+::::=+=-:-#- -%.
#> +*++==-=+= .:+::.#*==+=--:=+ -=@
#> **++===++=:+:::#- ==--*=#%-
#> #**++===:+:::#*++++--:-=++
#> +##**=:+:::#+==+*+--::=*
#> .=+:+:::#%%%#. -+::-=#
#> .:+:::#- =-:::-+#
#> .:::..#- =-::::+
#> .-==-==. -------
#-----------------------------------------------------------------------------
# Convert to 4 strings, each with 250 chars, so that we can loop
# over them on an 8-bit computer
#-----------------------------------------------------------------------------
rchars <- as.vector(t(im))
rmessage1 <- rchars[ 1:250] %>% paste(collapse='')
rmessage2 <- rchars[251:500] %>% paste(collapse='')
rmessage3 <- rchars[501:750] %>% paste(collapse='')
rmessage4 <- rchars[751:999] %>% paste(collapse='')
asm <- '*=$0801
.byte $0c, $08, $0a, $00, $9e, $20 ; 10 SYS 2080
.byte $32, $30, $38, $30, $00, $00
.byte $00
*=$0820
lda #$93 ; clear the screen
jsr $ffd2
lda #$0e ; Switch to lowercase/upperase character mode
jsr $ffd2
lda #$01 ; set text to white
sta $0286
ldy #$00 ; initialise offset index
loop1 lda message1,y ; load the character in message1 at this offset
jsr $ffd2 ; use kernal routine to put character on screen
iny ; repeat ...
cpy #$fa ; ... 250 times
bne loop1
ldy #$00 ; now do the same for message2 text
loop2
lda message2,y
jsr $ffd2
iny
cpy #$fa
bne loop2
ldy #$00 ; now do the same for message3 text
loop3
lda message3,y
jsr $ffd2
iny
cpy #$fa
bne loop3
ldy #$00 ; now do the same for message4 text
loop4
lda message4,y
jsr $ffd2
iny
cpy #$f9
bne loop4
wait
jmp wait
message1
.rtext rmessage1 ; ".rtext" instructions are directives to include the contents of a string from R at this location
message2
.rtext rmessage2
message3
.rtext rmessage3
message4
.rtext rmessage4
'