Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Programma in Assembly per Commodore 64
- ; Scritto da Kimono (F. Nardella)
- ; Visualizza e ruota un ottaedro in modalità bitmap (HiRes)
- ; Modificato per implementare il double buffer
- ; Le coordinate dei vertici del poliedro sono precalcolate e lette da memoria
- * = $C000
- ;******************************************************
- ; PARAMETRI VICII
- ;******************************************************
- VICII = $d000
- VICII1 = VICII + $11 ; VICII Control Register 1 - Hi Res - 53265
- VICIIA = VICII + $18 ; VICII Start Address Graphic Ram - 53272
- VICIIB = VICII + $20 ; VICII Border Color Registry - 53280
- VICIIS = VICII + $21 ; VICII BackGround Color Registry - 53281
- BITMAP1 = $2000 ; Prima area bitmap (8K)
- BITMAP2 = $6000 ; Seconda area bitmap (8K)
- switch_flag = $5f ; Flag per alternare le pagine
- loc = $5a ;loc e loc+1 sono LSB e MSB della locaz. in Graphic RAM in cui accendere il pixel
- store = $5c
- bmpage = $ff ; Variabile per la pagina bitmap attiva
- curpage = $fe ; Variabile per la pagina correntemente visualizzata
- FRAMEPTR = $4b ; Puntatore al frame corrente
- FRAMECNT = $4d ; Contatore dei frame
- PARAMS = $828
- xL = PARAMS + 0 ; LSB sono le coordinate per plot
- xH = PARAMS + 1 ; MSB sono le coordinate per plot
- yy = PARAMS + 2 ; sono le coordinate per plot
- x1 = PARAMS + 3 ; Coordinata X del primo punto
- y1 = PARAMS + 4 ; Coordinata Y del primo punto
- x2 = PARAMS + 5 ; Coordinata X del secondo punto
- y2 = PARAMS + 6 ; Coordinata Y del secondo punto
- dx = PARAMS + 7 ; Differenza X
- dy = PARAMS + 8 ; Differenza Y
- sx = PARAMS + 9 ; Direzione X (-1 o +1)
- sy = PARAMS + 10 ; Direzione Y (-1 o +1)
- err = PARAMS + 11 ; Errore accumulato
- START
- ldx #11
- stx VICIIB ; Bordo grigio scuro
- jsr HIRES ; Passa in modalità hi res
- jsr GRRAM ; Imposta la posizione della Bitmap nella seconda metà della Bank
- jsr CLEARCO1 ; Cancella e imposta la Color RAM 1
- jsr CLEARCO2 ; Cancella e imposta la Color RAM 2
- jsr INIT_DOUBLE_BUFFER ; Inizializza il double buffer
- jsr DRAW ; Disegna e ruota il poliedro
- rts
- INIT_DOUBLE_BUFFER
- ; Imposta il flag di switch iniziale
- lda #1
- sta switch_flag
- ; Configura la bank 0 (dove si trova BITMAP1)
- lda $dd00
- and #252 ; Pulisci i 2 bit di bank
- ora #3 ; Bank 0 ($2000-$FFF)
- sta $dd00
- ; Cancella entrambe le bitmap
- jsr CLEAR_BITMAP1
- jsr CLEAR_BITMAP2
- rts
- DRAW
- ; Inizializza il puntatore al primo frame
- lda #<COORD_TABLE
- sta FRAMEPTR
- lda #>COORD_TABLE
- sta FRAMEPTR+1
- ; Inizializza il contatore dei frame
- lda #0
- sta FRAMECNT
- ; Cancella la pagina attualmente non visualizzata
- jsr CLEAR_BITMAP1
- ; Imposta il buffer di disegno iniziale
- lda #>BITMAP1 ; Inizia disegnando sulla prima pagina
- sta bmpage
- ANIMATE
- jsr CLEAR_CURRENT_BITMAP ; Pulisce la bitmap corrente prima di disegnarci
- jsr DRAWFRAME ; Disegna il frame corrente nella pagina bmpage
- lda FRAMEPTR
- sta store+2
- ; Attendi la fine del frame (rasterline 0)
- lda #255
- WAIT_RASTER
- cmp VICII+$12 ; Confronta con il registro rasterline
- bne WAIT_RASTER
- ; Cambia le pagine
- jsr FLIP_PAGES
- ; Passa al frame successivo
- lda store+2
- clc
- adc #48 ; Ogni frame occupa 48 byte
- sta FRAMEPTR
- bcc _no_carry
- inc FRAMEPTR+1
- _no_carry
- ; Incrementa il contatore dei frame
- inc FRAMECNT
- lda FRAMECNT
- cmp #15
- bne ANIMATE
- lda #0
- sta FRAMECNT
- lda #<COORD_TABLE
- sta FRAMEPTR
- lda #>COORD_TABLE
- sta FRAMEPTR+1
- jmp ANIMATE
- ; --------------------------------------------------------------
- ; Funzione per disegnare un frame
- ; --------------------------------------------------------------
- DRAWFRAME
- ldy #0
- ldx #0
- draw_loop
- ; Leggi coordinate x1,y1
- lda (FRAMEPTR),y
- sta x1
- iny
- lda (FRAMEPTR),y
- sta y1
- iny
- ; Leggi coordinate x2,y2
- lda (FRAMEPTR),y
- sta x2
- iny
- lda (FRAMEPTR),y
- sta y2
- iny
- stx store+1 ; Salva X
- tya
- pha ; Salva Y
- jsr LINE ; Disegna la linea
- pla
- tay ; Recupera Y
- ldx store+1 ; Recupera X
- inx
- cpx #12 ; 12 linee per l'ottaedro
- bne draw_loop
- rts
- ; --------------------------------------------------------------
- ; Funzione per pulire la bitmap corrente
- ; --------------------------------------------------------------
- CLEAR_CURRENT_BITMAP
- ; Usa il valore in bmpage per determinare quale bitmap pulire
- lda bmpage
- cmp #>BITMAP1
- beq CLEAR_BITMAP1
- jmp CLEAR_BITMAP2
- ; --------------------------------------------------------------
- ; Funzione per attivare la modalità bitmap HiRes
- ; --------------------------------------------------------------
- HIRES
- lda VICII1
- ora #32 ; Imposta il bit per attivare la modalità bitmap
- sta VICII1
- rts
- GRRAM
- lda VICIIA
- ora #$08 ; Imposta il bit 3 per la prima pagina bitmap a $2000
- sta VICIIA
- sta curpage ; Memorizza l'impostazione corrente
- rts
- ; --------------------------------------------------------------
- ; Funzione per pulire lo schermo grafico
- ; --------------------------------------------------------------
- ; Routine per cancellare la prima bitmap
- CLEAR_BITMAP1
- lda #>BITMAP1
- sta $fb
- lda #<BITMAP1
- sta $fa
- jmp CLEARMEM
- ; Routine per cancellare la seconda bitmap
- CLEAR_BITMAP2
- lda #>BITMAP2
- sta $fb
- lda #<BITMAP2
- sta $fa
- ; Routine generale per cancellare 8KB di memoria
- CLEARMEM
- ldx #32 ; 32 pagine da 256 bytes = 8KB
- lda #0 ; Valore di cancellazione
- ldy #0
- clearmem_loop
- sta ($fa),y
- dey
- bne clearmem_loop
- inc $fb
- dex
- bne clearmem_loop
- rts
- ; --------------------------------------------------------------
- ; Funzione per pulire e impostare la Color RAM 1 ($0400)
- ; --------------------------------------------------------------
- CLEARCO1
- lda #208
- ldx #0
- stx $fa
- ldx #4 ; Imposta il byte alto dell'indirizzo in $FB a 4 (indirizzo $0400)
- stx $fb
- ldy #0
- cloop1
- sta ($fa),y
- iny
- bne cloop1
- inc $fb
- ldx $fb
- cpx #8 ; Controlla se ha raggiunto la pagina $0800
- bne cloop1
- rts
- ; --------------------------------------------------------------
- ; Funzione per pulire e impostare la Color RAM 2 ($4400)
- ; --------------------------------------------------------------
- CLEARCO2
- lda #208
- ldx #0
- stx $fa
- ldx #$44 ; Imposta il byte alto dell'indirizzo in $FB a 4 (indirizzo $4400)
- stx $fb
- ldy #0
- cloop2
- sta ($fa),y
- iny
- bne cloop2
- inc $fb
- ldx $fb
- cpx #$48 ; Controlla se ha raggiunto la pagina $4800
- bne cloop2
- rts
- ; --------------------------------------------------------------
- ; Routine per alternare tra le pagine bitmap
- ; --------------------------------------------------------------
- FLIP_PAGES
- lda switch_flag
- eor #1 ; Inverti il flag
- sta switch_flag
- beq USE_BITMAP1
- ; Passa a BITMAP2 (bank 1)
- lda $dd00
- and #252
- ora #2 ; Bank 1 ($4000-$7FFF)
- sta $dd00
- ; Imposta il puntatore per il disegno su BITMAP1
- lda #>BITMAP1
- sta bmpage
- jmp DONE_FLIP
- USE_BITMAP1
- ; Passa a BITMAP1 (bank 0)
- lda $dd00
- and #252
- ora #3 ; Bank 0 ($0000-$3FFF)
- sta $dd00
- ; Imposta il puntatore per il disegno su BITMAP2
- lda #>BITMAP2
- sta bmpage
- DONE_FLIP
- rts
- ; --------------------------------------------------------------
- ; Funzione per disegnare un punto
- ; --------------------------------------------------------------
- PLOT
- lda xL
- and #7 ; Mantiene solo i 3 bit meno significativi (0-7)
- tax
- lda #0
- sta loc
- lda xL
- and #$f8 ; Mantiene solo i 5 bit più significativi (multipli di 8)
- sta store
- lda yy
- lsr ; Shifta a destra (divide per 2)
- lsr ; Shifta a destra nuovamente (divide per 2 ancora)
- lsr ; Shifta a destra nuovamente (divide per 8 in totale)
- sta loc+1 ; Memorizza in loc+1 (parte alta dell'indirizzo)
- lsr ; Shifta a destra ancora (divide per 16 in totale)
- ror loc ; Ruota a destra con il carry in loc
- lsr ; Shifta a destra ancora (divide per 32 in totale)
- ror loc ; Ruota a destra con il carry in loc ancora
- adc loc+1 ; Somma loc+1 al registro A
- sta loc+1
- lda yy
- and #7 ; Mantiene solo i 3 bit meno significativi (0-7)
- adc loc ; Somma al valore di loc
- adc store ; Somma il valore di store (posizione x)
- sta loc ; Memorizza il risultato finale in loc
- lda loc+1
- adc xH ; Aggiunge il byte alto di x (per coordinate x > 255)
- adc bmpage ; Aggiunge l'offset della pagina della bitmap
- sta loc+1 ; Memorizza il risultato finale in loc+1 (completa l'indirizzo)
- ldy #0
- lda (loc),y
- ora BITMASK,x ; Esegue un OR con il bit dalla tabella BITMASK
- sta (loc),y
- rts
- ; --------------------------------------------------------------
- ; Funzione per disegnare una linea tra due punti
- ; --------------------------------------------------------------
- LINE
- jsr SUB_LINE
- ; Calcola err = (dx > dy ? dx : dy) / 2
- lda dx
- cmp dy
- bcc dy_greater
- lsr ; Dividi dx per 2
- sta err
- jmp check_slopes
- dy_greater
- lda dy
- lsr ; Dividi dy per 2
- sta err
- check_slopes
- ; Decidi quale routine di loop usare
- lda dx
- cmp dy
- bcs dx_ge_dy ; Se dx >= dy, usa la routine x-dominant
- ; Routine y-dominant (pendenza > 1)
- y_loop
- jsr PLOT ; Disegna il punto
- ; Aggiorna l'errore e muovi x se necessario
- lda err
- clc
- adc dx ; err += dx
- sta err
- cmp dy ; Se err >= dy...
- bcc skip_x_step
- sec
- sbc dy ; ...sottrai dy da err
- sta err
- lda xL ; ...e incrementa/decrementa x
- clc
- adc sx
- sta xL
- bcc skip_x_step
- inc xH ; Gestisci il riporto se necessario
- skip_x_step
- ; Incrementa o decrementa y
- lda yy
- clc
- adc sy
- sta yy
- ; Controlla se abbiamo raggiunto y2
- cmp y2
- bne y_loop ; Se y != y2, continua
- lda xL
- cmp x2
- bne y_loop ; Se x != x2, continua
- rts
- dx_ge_dy
- ; Routine x-dominant (pendenza <= 1)
- x_loop
- jsr PLOT ; Disegna il punto
- ; Aggiorna l'errore e muovi y se necessario
- lda err
- clc
- adc dy ; err += dy
- sta err
- cmp dx ; Se err >= dx...
- bcc skip_y_step
- sec
- sbc dx ; ...sottrai dx da err
- sta err
- lda yy ; ...e incrementa/decrementa y
- clc
- adc sy
- sta yy
- skip_y_step
- ; Incrementa o decrementa x
- lda xL
- clc
- adc sx
- sta xL
- bcc no_carry
- inc xH ; Gestisci il riporto se necessario
- no_carry
- ; Controlla se abbiamo raggiunto x2
- cmp x2
- bne x_loop ; Se x != x2, continua
- lda yy
- cmp y2
- bne x_loop ; Se y != y2, continua
- rts
- SUB_LINE
- ; Controlla se x1 > x2 e in tal caso scambia i punti
- lda x1
- cmp x2
- bcc no_swap_x ; Se x1 < x2, non serve scambiare
- ; Scambia x1 e x2
- lda x1
- pha ; Salva x1 nello stack
- lda x2
- sta x1
- pla
- sta x2
- ; Scambia anche y1 e y2
- lda y1
- pha ; Salva y1 nello stack
- lda y2
- sta y1
- pla
- sta y2
- no_swap_x
- ; Calcola dx = abs(x2 - x1)
- lda x2
- sec
- sbc x1
- bpl store_dx ; Se è positivo, salta
- eor #$FF ; Altrimenti, negalo (complemento a 1)
- clc
- adc #1 ; Complemento a 2 per ottenere abs()
- store_dx
- sta dx
- ; Calcola dy = abs(y2 - y1)
- lda y2
- sec
- sbc y1
- bpl store_dy ; Se è positivo, salta
- eor #$FF ; Altrimenti, negalo
- clc
- adc #1
- store_dy
- sta dy
- ; Determina direzione x (sx)
- lda x1
- cmp x2
- bcc x_ascending
- lda #$FF ; sx = -1 (x1 > x2)
- jmp store_sx
- x_ascending
- lda #$01 ; sx = 1 (x1 < x2)
- store_sx
- sta sx
- ; Determina direzione y (sy)
- lda y1
- cmp y2
- bcc y_ascending
- lda #$FF ; sy = -1 (y1 > y2)
- jmp store_sy
- y_ascending
- lda #$01 ; sy = 1 (y1 < y2)
- store_sy
- sta sy
- ; Imposta i valori iniziali di x e y
- lda x1
- sta xL
- lda #0 ; Assumiamo che xH sia sempre 0 per semplicità
- sta xH
- lda y1
- sta yy
- rts
- BITMASK
- .byte $80, $40, $20, $10, $08, $04, $02, $01
- COORD_TABLE
- ; Frame 0
- .byte 210, 100, 160, 150
- .byte 210, 100, 160, 50
- .byte 210, 100, 160, 100
- .byte 210, 100, 160, 100
- .byte 110, 100, 160, 150
- .byte 110, 100, 160, 50
- .byte 110, 100, 160, 100
- .byte 110, 100, 160, 100
- .byte 160, 150, 160, 100
- .byte 160, 150, 160, 100
- .byte 160, 50, 160, 100
- .byte 160, 50, 160, 100
- ; Frame 1
- .byte 211, 100, 161, 146
- .byte 211, 100, 158, 48
- .byte 211, 100, 164, 91
- .byte 211, 100, 153, 113
- .byte 111, 100, 161, 146
- .byte 111, 100, 158, 48
- .byte 111, 100, 164, 91
- .byte 111, 100, 153, 113
- .byte 161, 146, 164, 91
- .byte 161, 146, 153, 113
- .byte 158, 48, 164, 91
- .byte 158, 48, 153, 113
- ; Frame 2
- .byte 211, 100, 163, 141
- .byte 211, 100, 155, 49
- .byte 211, 100, 167, 83
- .byte 211, 100, 147, 126
- .byte 113, 100, 163, 141
- .byte 113, 100, 155, 49
- .byte 113, 100, 167, 83
- .byte 113, 100, 147, 126
- .byte 163, 141, 167, 83
- .byte 163, 141, 147, 126
- .byte 155, 49, 167, 83
- .byte 155, 49, 147, 126
- ; Frame 3
- .byte 211, 100, 167, 135
- .byte 211, 100, 149, 52
- .byte 211, 100, 170, 75
- .byte 211, 100, 144, 136
- .byte 115, 100, 167, 135
- .byte 115, 100, 149, 52
- .byte 115, 100, 170, 75
- .byte 115, 100, 144, 136
- .byte 167, 135, 170, 75
- .byte 167, 135, 144, 136
- .byte 149, 52, 170, 75
- .byte 149, 52, 144, 136
- ; Frame 4
- .byte 210, 100, 172, 128
- .byte 210, 100, 141, 59
- .byte 210, 100, 171, 67
- .byte 210, 100, 143, 143
- .byte 118, 100, 172, 128
- .byte 118, 100, 141, 59
- .byte 118, 100, 171, 67
- .byte 118, 100, 143, 143
- .byte 172, 128, 171, 67
- .byte 172, 128, 143, 143
- .byte 141, 59, 171, 67
- .byte 141, 59, 143, 143
- ; Frame 5
- .byte 209, 100, 178, 121
- .byte 209, 100, 133, 69
- .byte 209, 100, 171, 60
- .byte 209, 100, 145, 148
- .byte 121, 100, 178, 121
- .byte 121, 100, 133, 69
- .byte 121, 100, 171, 60
- .byte 121, 100, 145, 148
- .byte 178, 121, 171, 60
- .byte 178, 121, 145, 148
- .byte 133, 69, 171, 60
- .byte 133, 69, 145, 148
- ; Frame 6
- .byte 207, 100, 183, 112
- .byte 207, 100, 125, 80
- .byte 207, 100, 168, 55
- .byte 207, 100, 150, 150
- .byte 124, 100, 183, 112
- .byte 124, 100, 125, 80
- .byte 124, 100, 168, 55
- .byte 124, 100, 150, 150
- .byte 183, 112, 168, 55
- .byte 183, 112, 150, 150
- .byte 125, 80, 168, 55
- .byte 125, 80, 150, 150
- ; Frame 7
- .byte 204, 100, 188, 104
- .byte 204, 100, 119, 93
- .byte 204, 100, 163, 51
- .byte 204, 100, 156, 150
- .byte 128, 100, 188, 104
- .byte 128, 100, 119, 93
- .byte 128, 100, 163, 51
- .byte 128, 100, 156, 150
- .byte 188, 104, 163, 51
- .byte 188, 104, 156, 150
- .byte 119, 93, 163, 51
- .byte 119, 93, 156, 150
- ; Frame 8
- .byte 201, 100, 191, 95
- .byte 201, 100, 115, 106
- .byte 201, 100, 156, 49
- .byte 201, 100, 163, 148
- .byte 131, 100, 191, 95
- .byte 131, 100, 115, 106
- .byte 131, 100, 156, 49
- .byte 131, 100, 163, 148
- .byte 191, 95, 156, 49
- .byte 191, 95, 163, 148
- .byte 115, 106, 156, 49
- .byte 115, 106, 163, 148
- ; Frame 9
- .byte 196, 100, 193, 86
- .byte 196, 100, 115, 117
- .byte 196, 100, 146, 50
- .byte 196, 100, 171, 145
- .byte 135, 100, 193, 86
- .byte 135, 100, 115, 117
- .byte 135, 100, 146, 50
- .byte 135, 100, 171, 145
- .byte 193, 86, 146, 50
- .byte 193, 86, 171, 145
- .byte 115, 117, 146, 50
- .byte 115, 117, 171, 145
- ; Frame 10
- .byte 191, 100, 193, 77
- .byte 191, 100, 117, 128
- .byte 191, 100, 136, 53
- .byte 191, 100, 180, 140
- .byte 139, 100, 193, 77
- .byte 139, 100, 117, 128
- .byte 139, 100, 136, 53
- .byte 139, 100, 180, 140
- .byte 193, 77, 136, 53
- .byte 193, 77, 180, 140
- .byte 117, 128, 136, 53
- .byte 117, 128, 180, 140
- ; Frame 11
- .byte 186, 100, 191, 68
- .byte 186, 100, 123, 136
- .byte 186, 100, 127, 60
- .byte 186, 100, 188, 134
- .byte 143, 100, 191, 68
- .byte 143, 100, 123, 136
- .byte 143, 100, 127, 60
- .byte 143, 100, 188, 134
- .byte 191, 68, 127, 60
- .byte 191, 68, 188, 134
- .byte 123, 136, 127, 60
- .byte 123, 136, 188, 134
- ; Frame 12
- .byte 180, 100, 186, 61
- .byte 180, 100, 130, 142
- .byte 180, 100, 118, 68
- .byte 180, 100, 196, 127
- .byte 147, 100, 186, 61
- .byte 147, 100, 130, 142
- .byte 147, 100, 118, 68
- .byte 147, 100, 196, 127
- .byte 186, 61, 118, 68
- .byte 186, 61, 196, 127
- .byte 130, 142, 118, 68
- .byte 130, 142, 196, 127
- ; Frame 13
- .byte 173, 100, 179, 55
- .byte 173, 100, 139, 146
- .byte 173, 100, 113, 78
- .byte 173, 100, 202, 119
- .byte 151, 100, 179, 55
- .byte 151, 100, 139, 146
- .byte 151, 100, 113, 78
- .byte 151, 100, 202, 119
- .byte 179, 55, 113, 78
- .byte 179, 55, 202, 119
- .byte 139, 146, 113, 78
- .byte 139, 146, 202, 119
- ; Frame 14
- .byte 166, 100, 170, 51
- .byte 166, 100, 149, 149
- .byte 166, 100, 110, 89
- .byte 166, 100, 207, 110
- .byte 155, 100, 170, 51
- .byte 155, 100, 149, 149
- .byte 155, 100, 110, 89
- .byte 155, 100, 207, 110
- .byte 170, 51, 110, 89
- .byte 170, 51, 207, 110
- .byte 149, 149, 110, 89
- .byte 149, 149, 207, 110
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement