Scripted Table Generation

I program in Kotlin for my day job and I like how lambdas and extension methods simplify tasks. I decided to use Kotlin to generate some tabled data for my 6502 project.

Generating some random screen x and y positions

I needed tables that would convert an 8-bit value to a screen char position in the x (0-39) or y (0-24) values. For a quick-and-dirty solution I generated two 256 byte tables using Kotlin’s random Int generation. It’s fast to grab a random number in the 0-255 range by polling the raster count address ($d012). Clamping that value to a smaller range is more challenging and that’s where the tables become useful.

As you can see from the example below, the raster value is used as an index into a table where each entry is in the range 0-39. The overall quality of randomness could be considered poor because of the distribution of these random numbers over such a small quantity. However, for my simple use the set is fine.

fun main(){
    val randomScreenXValues = List(256){Random.nextInt(0,40)}
    println(randomScreenXValues.joinToString(separator = ",", prefix = "randomScreenXValues ${'\t'} byte ", transform = {
            value -> "$%02x".format(value)
    }))

    val randomScreenYValues = List(256){Random.nextInt(0, 25)}
    println(randomScreenYValues.joinToString(separator = ",", prefix = "randomScreenYValues ${'\t'} byte ", transform = {
            value -> "$%02x".format(value)
    }))
}

And the output:

randomScreenXValues      byte $0e,$0b,$0a,$09,$1e,$21,$06,$14,$06,$25,$02,$0a,$0f,$0e,$26,$0a,$09,$16,$00,$24,$22,$12,$11,$1e,$1c,$1c,$07,$1b,$04,$24,$1d,$1e,$06,$17,$1d,$1e,$0d,$10,$15,$24,$00,$12,$0b,$1d,$0b,$1e,$13,$27,$0e,$07,$1d,$17,$11,$16,$06,$0e,$23,$16,$15,$19,$25,$0e,$0e,$02,$1f,$1e,$25,$04,$14,$13,$23,$0a,$1b,$1f,$00,$20,$15,$11,$12,$0f,$0c,$16,$1e,$20,$00,$21,$04,$10,$0b,$04,$17,$1f,$1d,$24,$22,$05,$14,$04,$06,$1c,$18,$17,$05,$19,$25,$0d,$03,$12,$04,$14,$01,$1d,$21,$17,$14,$0f,$00,$16,$25,$00,$18,$04,$02,$11,$18,$0c,$20,$06,$09,$25,$10,$03,$0c,$08,$08,$20,$01,$0e,$16,$1d,$1d,$0e,$04,$25,$0b,$01,$11,$0c,$04,$03,$0a,$1c,$23,$17,$16,$0d,$15,$17,$05,$24,$20,$15,$1a,$25,$20,$26,$27,$09,$06,$0b,$25,$1e,$02,$24,$1a,$21,$18,$24,$07,$21,$1a,$26,$22,$14,$09,$04,$21,$24,$22,$10,$0a,$04,$01,$21,$04,$12,$10,$0b,$17,$09,$16,$06,$08,$17,$01,$1c,$25,$14,$11,$06,$23,$0d,$19,$02,$0f,$1e,$1c,$13,$10,$13,$03,$23,$23,$1d,$13,$21,$1f,$06,$13,$05,$09,$0a,$25,$01,$0d,$26,$1c,$08,$01,$18,$1d,$1d,$1e,$23,$26,$06,$27,$15,$17,$01,$1f,$23,$0d,$1e,$1c,$13
randomScreenYValues      byte $11,$15,$15,$06,$00,$0d,$02,$11,$08,$01,$14,$13,$09,$08,$03,$14,$11,$01,$06,$0b,$03,$09,$08,$0f,$03,$02,$00,$0a,$0d,$0c,$0e,$0a,$07,$03,$08,$0e,$08,$10,$06,$14,$0b,$0a,$07,$0d,$18,$03,$0c,$0e,$02,$16,$0a,$10,$02,$10,$06,$0f,$17,$18,$12,$0a,$04,$18,$12,$14,$08,$15,$11,$0a,$0b,$02,$04,$07,$02,$02,$17,$08,$0b,$12,$0e,$0c,$0c,$0d,$09,$14,$0b,$0b,$0b,$0c,$0a,$0f,$03,$15,$17,$01,$0a,$11,$05,$15,$03,$03,$01,$08,$05,$03,$11,$12,$0e,$0c,$01,$01,$14,$0f,$0f,$0f,$04,$03,$18,$02,$10,$03,$07,$15,$00,$05,$18,$11,$17,$13,$07,$00,$05,$0e,$07,$00,$17,$04,$10,$0a,$14,$11,$06,$0b,$0f,$0f,$0f,$18,$16,$06,$16,$07,$17,$08,$09,$10,$15,$08,$06,$05,$0e,$15,$17,$00,$15,$18,$0a,$11,$00,$08,$09,$10,$14,$18,$16,$05,$16,$17,$0a,$00,$18,$10,$11,$00,$00,$03,$11,$16,$0a,$06,$0e,$17,$0f,$16,$02,$18,$10,$12,$14,$0b,$02,$0a,$13,$03,$0e,$09,$16,$09,$17,$14,$0b,$0c,$03,$0c,$0f,$14,$11,$11,$03,$01,$18,$0f,$0a,$15,$17,$0c,$13,$10,$16,$0c,$0b,$11,$13,$0d,$02,$03,$05,$03,$0e,$02,$12,$14,$01,$16,$17,$0d,$11,$15,$16,$04,$0a,$0d,$17,$04,$0c,$16,$05,$12

;using the current raster value, look up values in the x and y tables
set_item_position
        ldx $d012
        lda randomScreenXValues,x
        sta itemX
        lda randomScreenYValues,x
        sta itemY
        rts

In a future post I’ll put these tables to use. If you want to generate similar tables, you can run the code above in the Kotlin Playground REPL. Here’s a link to the code to get you started https://pl.kotl.in/ZTfMWM8qX

Leave a Reply

Your email address will not be published. Required fields are marked *