; Rutina para obtener un área de color en alta resolución (HR) centrada en la pantalla.
; El área obtenida es de 112x192 píxeles (14 columnas de atributos X 192 atributos).

; (c)2009 Miguel Angel Rodríguez Jódar. http://www.zxprojects.com
; Liberado bajo licencia GPL.


; Rutinas:
;   Init: habilitar el modo de alta resolución. Debe llamarse antes que cualquier otra rutina.
;   PokeAttr: pone un valor de atributo en las coordenadas de pantalla especificadas.
;   PeekAttr: lee un valor de atributo en las coordenadas de pantalla especificadas.
;   Demo1: una pequeña demo que usa PokeAttr para mostrar un bitmap en alta resolución de color,
;          usando una máscara tipo 00001111 para obtener 28 píxeles horizontales en HR.

; --------------- MACROS --------------------

; Espera a que comience un retrazo horizontal (es decir, a que la ULA comience a generar la parte derecha
; del borde)
WaitBHRetrace   macro
                local Wait
Wait            in a,(c)
                jp p,Wait
                endm

; Espera a que termine un retrazo horizontal (es decir, a que la ULA termine de generar la parte izquierda
; del borde y comience a leer la pantalla)
WaitEHRetrace   macro
                local Wait
Wait            in a,(c)
                jp m,Wait
                endm

; Macro que transfiere el scan inicial. Se ejecuta cuando aun la ULA está pintando el borde superior, así
; que tiene que esperarse a que la ULA termine de pintar el borde, y haya terminado justo de pintar el
; primer scan.
TransferI       macro attr16,hasta
                  ld sp,hasta+23    ;posicion siguiente a la última columna de atributos a rellenar
                  rept 7
                    ld hl,attr16
                    push hl         ;rellenamos hacia atrás, hasta la columna 9. TOTAL: 21 ciclos para cada 2 bytes.
                  endm
                  WaitEHRetrace     ;esperamos hasta saber que la ULA ha empezado a leer el primer scan
                  rept 7    ;TOTAL: 28 ciclos de retardo.
                    nop
                  endm
                endm

; Macro que transfiere cualquier otro scan. Si la temporización es correcta, esta macro comienza a ejecutarse
; justo después de que la ULA haya terminado de leer la última columna del área de HR del scan anterior.
Transfer        macro attr16,hasta
                  ld sp,hasta+23   ;posicion siguiente a la última columna de atributos a rellenar
                  rept 7
                    ld hl,attr16
                    push hl        ;rellenamos hacia atrás, hasta la columna 9. TOTAL: 21 ciclos para cada 2 bytes.
                  endm
                  rept 8
                    nop
                  endm
                  ld b,255
                  ld b,255
                  ld b,255   ;TOTAL: 32+21 ciclos de espera
                endm



; ------ DEMO: CARGA UN BITMAP DE 28X192 PIXELES CON 8 COLORES -------
; NOTA: sería conveniente hacer un PAUSE 0 después de mostrar esta demo (con el RAND USR correspondiente)
; para que el mensaje de error no machaque el área de pantalla de las dos últimas líneas.
                org 32768
Demo1           proc
                xor a
                out (254),a
                ld hl,22528
                ld de,22529
                ld bc,767
                ld (hl),a
                di     ; para que la rutina de HR no "enguarre" la pantalla.
                ldir   ; pantalla completamente en negro.

                ld hl,16384
                ld de,16385
                ld bc,6143
                ld (hl),00001111b   ;patron 00001111 para los bitmaps
                ldir
                ei

                ld hl,Natalie
                ld c,0              ;bucle externo C=0 hasta 191
BucY            push bc
                ld b,0              ;bucle interno B=0 hasta 22
BucX            push bc
                ld a,(hl)           ;leemos valor para PAPER...
                sla a
                sla a
                sla a               ;y lo ponemos en su sitio.
                inc hl
                ld d,(hl)           ;leemos valor de INK
                inc hl
                or d                ;y lo combinamos con el PAPER de antes para obtener el atributo completo.
                push hl
                call PokeAttr       ;y lo enviamos al archivo de atributos en HR
                pop hl
                pop bc
                inc b
                ld a,b
                cp 14
                jr nz,BucX
                pop bc
                inc c
                ld a,c
                cp 192
                jr nz,BucY
                ret
                endp

; ---------------- BITMAP de 28x192 pixeles --------------------
Natalie		db 4,3,0,4,3,0,7,0,0,6,1,6,2,3,7,6,7,3,7,7
		db 0,7,0,2,4,2,0,7,2,0,6,1,6,0,2,4,3,0,2,0
		db 4,7,6,7,7,6,7,2,6,0,0,7,0,3,4,0,0,7,0,2
		db 4,2,5,0,2,6,0,7,2,7,1,7,6,3,7,6,5,2,4,2
		db 0,6,0,6,4,2,0,6,1,6,0,2,5,6,2,0,6,7,7,6
		db 5,7,6,3,6,3,0,4,2,5,2,1,2,1,6,0,2,0,7,0
		db 6,1,2,4,3,6,7,7,3,6,5,7,6,0,7,0,2,4,2,4
		db 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ION NUEVA IM2 ----------
                org 0c0ffh
                dw Start
GuardaSP        dw 0

Init            ld a,0c0h
                ld i,a
                im 2

                call Demo1
                ret

Start           di
                push af
                push bc
                push hl
                ld (GuardaSP),sp
;------------------------------------------------------
                ld bc,0ffffh   ;para acceder al puerto 255

;A continuación se ejecutan 192 macros, una por cada scan. Cada macro expande
;a 7 veces lo siguiente:
;  LD HL,attr1+256*attr2
;  PUSH HL
;Donde attr1 y attr2 son dos bytes de atributos. Cada secuencia suponen 21 ciclos
;de reloj (más lo que añada la ULA por acceso a memoria contenida).
;Los valores iniciales que se guardan dan como resultado un "gradiente" de 8 colores
;desde el negro al blanco, pasando por todos los que hay en el Spectrum.

                ;Linea 0
sc000           TransferI 0707h,22528
sc001           Transfer  0e0eh,22528
sc002           Transfer  1515h,22528
sc003           Transfer  1c1ch,22528
sc004           Transfer  6363h,22528
sc005           Transfer  6a6ah,22528
sc006           Transfer  7171h,22528
sc007           Transfer  7878h,22528

                ;Linea 1
sc008           Transfer  0707h,22528+32*1
sc009           Transfer  0e0eh,22528+32*1
sc010           Transfer  1515h,22528+32*1
sc011           Transfer  1c1ch,22528+32*1
sc012           Transfer  6363h,22528+32*1
sc013           Transfer  6a6ah,22528+32*1
sc014           Transfer  7171h,22528+32*1
sc015           Transfer  7878h,22528+32*1

                ;Linea 2
sc016           Transfer  0707h,22528+32*2
sc017           Transfer  0e0eh,22528+32*2
sc018           Transfer  1515h,22528+32*2
sc019           Transfer  1c1ch,22528+32*2
sc020           Transfer  6363h,22528+32*2
sc021           Transfer  6a6ah,22528+32*2
sc022           Transfer  7171h,22528+32*2
sc023           Transfer  7878h,22528+32*2

                ;Linea 3
sc024           Transfer  0707h,22528+32*3
sc025           Transfer  0e0eh,22528+32*3
sc026           Transfer  1515h,22528+32*3
sc027           Transfer  1c1ch,22528+32*3
sc028           Transfer  6363h,22528+32*3
sc029           Transfer  6a6ah,22528+32*3
sc030           Transfer  7171h,22528+32*3
sc031           Transfer  7878h,22528+32*3

                ;Linea 4
sc032           Transfer  0707h,22528+32*4
sc033           Transfer  0e0eh,22528+32*4
sc034           Transfer  1515h,22528+32*4
sc035           Transfer  1c1ch,22528+32*4
sc036           Transfer  6363h,22528+32*4
sc037           Transfer  6a6ah,22528+32*4
sc038           Transfer  7171h,22528+32*4
sc039           Transfer  7878h,22528+32*4

                ;Linea 5
sc040           Transfer  0707h,22528+32*5
sc041           Transfer  0e0eh,22528+32*5
sc042           Transfer  1515h,22528+32*5
sc043           Transfer  1c1ch,22528+32*5
sc044           Transfer  6363h,22528+32*5
sc045           Transfer  6a6ah,22528+32*5
sc046           Transfer  7171h,22528+32*5
sc047           Transfer  7878h,22528+32*5

                ;Linea 6
sc048           Transfer  0707h,22528+32*6
sc049           Transfer  0e0eh,22528+32*6
sc050           Transfer  1515h,22528+32*6
sc051           Transfer  1c1ch,22528+32*6
sc052           Transfer  6363h,22528+32*6
sc053           Transfer  6a6ah,22528+32*6
sc054           Transfer  7171h,22528+32*6
sc055           Transfer  7878h,22528+32*6

                ;Linea 7
sc056           Transfer  0707h,22528+32*7
sc057           Transfer  0e0eh,22528+32*7
sc058           Transfer  1515h,22528+32*7
sc059           Transfer  1c1ch,22528+32*7
sc060           Transfer  6363h,22528+32*7
sc061           Transfer  6a6ah,22528+32*7
sc062           Transfer  7171h,22528+32*7
sc063           Transfer  7878h,22528+32*7

                ;Linea 8
sc064           Transfer  0707h,22528+32*8
sc065           Transfer  0e0eh,22528+32*8
sc066           Transfer  1515h,22528+32*8
sc067           Transfer  1c1ch,22528+32*8
sc068           Transfer  6363h,22528+32*8
sc069           Transfer  6a6ah,22528+32*8
sc070           Transfer  7171h,22528+32*8
sc071           Transfer  7878h,22528+32*8

                ;Linea 9
sc072           Transfer  0707h,22528+32*9
sc073           Transfer  0e0eh,22528+32*9
sc074           Transfer  1515h,22528+32*9
sc075           Transfer  1c1ch,22528+32*9
sc076           Transfer  6363h,22528+32*9
sc077           Transfer  6a6ah,22528+32*9
sc078           Transfer  7171h,22528+32*9
sc079           Transfer  7878h,22528+32*9

                ;Linea 10
sc080           Transfer  0707h,22528+32*10
sc081           Transfer  0e0eh,22528+32*10
sc082           Transfer  1515h,22528+32*10
sc083           Transfer  1c1ch,22528+32*10
sc084           Transfer  6363h,22528+32*10
sc085           Transfer  6a6ah,22528+32*10
sc086           Transfer  7171h,22528+32*10
sc087           Transfer  7878h,22528+32*10

                ;Linea 11
sc088           Transfer  0707h,22528+32*11
sc089           Transfer  0e0eh,22528+32*11
sc090           Transfer  1515h,22528+32*11
sc091           Transfer  1c1ch,22528+32*11
sc092           Transfer  6363h,22528+32*11
sc093           Transfer  6a6ah,22528+32*11
sc094           Transfer  7171h,22528+32*11
sc095           Transfer  7878h,22528+32*11

                ;Linea 12
sc096           Transfer  0707h,22528+32*12
sc097           Transfer  0e0eh,22528+32*12
sc098           Transfer  1515h,22528+32*12
sc099           Transfer  1c1ch,22528+32*12
sc100           Transfer  6363h,22528+32*12
sc101           Transfer  6a6ah,22528+32*12
sc102           Transfer  7171h,22528+32*12
sc103           Transfer  7878h,22528+32*12

                ;Linea 13
sc104           Transfer  0707h,22528+32*13
sc105           Transfer  0e0eh,22528+32*13
sc106           Transfer  1515h,22528+32*13
sc107           Transfer  1c1ch,22528+32*13
sc108           Transfer  6363h,22528+32*13
sc109           Transfer  6a6ah,22528+32*13
sc110           Transfer  7171h,22528+32*13
sc111           Transfer  7878h,22528+32*13

                ;Linea 14
sc112           Transfer  0707h,22528+32*14
sc113           Transfer  0e0eh,22528+32*14
sc114           Transfer  1515h,22528+32*14
sc115           Transfer  1c1ch,22528+32*14
sc116           Transfer  6363h,22528+32*14
sc117           Transfer  6a6ah,22528+32*14
sc118           Transfer  7171h,22528+32*14
sc119           Transfer  7878h,22528+32*14

                ;Linea 15
sc120           Transfer  0707h,22528+32*15
sc121           Transfer  0e0eh,22528+32*15
sc122           Transfer  1515h,22528+32*15
sc123           Transfer  1c1ch,22528+32*15
sc124           Transfer  6363h,22528+32*15
sc125           Transfer  6a6ah,22528+32*15
sc126           Transfer  7171h,22528+32*15
sc127           Transfer  7878h,22528+32*15

                ;Linea 16
sc128           Transfer  0707h,22528+32*16
sc129           Transfer  0e0eh,22528+32*16
sc130           Transfer  1515h,22528+32*16
sc131           Transfer  1c1ch,22528+32*16
sc132           Transfer  6363h,22528+32*16
sc133           Transfer  6a6ah,22528+32*16
sc134           Transfer  7171h,22528+32*16
sc135           Transfer  7878h,22528+32*16

                ;Linea 17
sc136           Transfer  0707h,22528+32*17
sc137           Transfer  0e0eh,22528+32*17
sc138           Transfer  1515h,22528+32*17
sc139           Transfer  1c1ch,22528+32*17
sc140           Transfer  6363h,22528+32*17
sc141           Transfer  6a6ah,22528+32*17
sc142           Transfer  7171h,22528+32*17
sc143           Transfer  7878h,22528+32*17

                ;Linea 18
sc144           Transfer  0707h,22528+32*18
sc145           Transfer  0e0eh,22528+32*18
sc146           Transfer  1515h,22528+32*18
sc147           Transfer  1c1ch,22528+32*18
sc148           Transfer  6363h,22528+32*18
sc149           Transfer  6a6ah,22528+32*18
sc150           Transfer  7171h,22528+32*18
sc151           Transfer  7878h,22528+32*18

                ;Linea 19
sc152           Transfer  0707h,22528+32*19
sc153           Transfer  0e0eh,22528+32*19
sc154           Transfer  1515h,22528+32*19
sc155           Transfer  1c1ch,22528+32*19
sc156           Transfer  6363h,22528+32*19
sc157           Transfer  6a6ah,22528+32*19
sc158           Transfer  7171h,22528+32*19
sc159           Transfer  7878h,22528+32*19

                ;Linea 20
sc160           Transfer  0707h,22528+32*20
sc161           Transfer  0e0eh,22528+32*20
sc162           Transfer  1515h,22528+32*20
sc163           Transfer  1c1ch,22528+32*20
sc164           Transfer  6363h,22528+32*20
sc165           Transfer  6a6ah,22528+32*20
sc166           Transfer  7171h,22528+32*20
sc167           Transfer  7878h,22528+32*20

                ;Linea 21
sc168           Transfer  0707h,22528+32*21
sc169           Transfer  0e0eh,22528+32*21
sc170           Transfer  1515h,22528+32*21
sc171           Transfer  1c1ch,22528+32*21
sc172           Transfer  6363h,22528+32*21
sc173           Transfer  6a6ah,22528+32*21
sc174           Transfer  7171h,22528+32*21
sc175           Transfer  7878h,22528+32*21

                ;Linea 22
sc176           Transfer  0707h,22528+32*22
sc177           Transfer  0e0eh,22528+32*22
sc178           Transfer  1515h,22528+32*22
sc179           Transfer  1c1ch,22528+32*22
sc180           Transfer  6363h,22528+32*22
sc181           Transfer  6a6ah,22528+32*22
sc182           Transfer  7171h,22528+32*22
sc183           Transfer  7878h,22528+32*22

                ;Linea 23
sc184           Transfer  0707h,22528+32*23
sc185           Transfer  0e0eh,22528+32*23
sc186           Transfer  1515h,22528+32*23
sc187           Transfer  1c1ch,22528+32*23
sc188           Transfer  6363h,22528+32*23
sc189           Transfer  6a6ah,22528+32*23
sc190           Transfer  7171h,22528+32*23
sc191           Transfer  7878h,22528+32*23

;------------------------------------------------------
                ld sp,(GuardaSP)
                pop hl
                pop bc
                pop af

                ei
                rst #38
                reti

; Tabla que guarda la dirección de comienzo de cada macro expandida en el código.
DirScans        dw sc000,sc001,sc002,sc003,sc004,sc005,sc006,sc007,sc008,sc009,sc010,sc011,sc012,sc013,sc014,sc015,sc016,sc017,sc018,sc019,sc020,sc021,sc022,sc023,sc024,sc025,sc026,sc027,sc028,sc029,sc030,sc031,sc032,sc033,sc034,sc035,sc036,sc037,sc038,sc039,sc040,sc041,sc042,sc043,sc044,sc045,sc046,sc047,sc048,sc049,sc050,sc051,sc052,sc053,sc054,sc055,sc056,sc057,sc058,sc059,sc060,sc061,sc062,sc063,sc064,sc065,sc066,sc067,sc068,sc069,sc070,sc071,sc072,sc073,sc074,sc075,sc076,sc077,sc078,sc079,sc080,sc081,sc082,sc083,sc084,sc085,sc086,sc087,sc088,sc089,sc090,sc091,sc092,sc093,sc094,sc095,sc096,sc097,sc098,sc099,sc100,sc101,sc102,sc103,sc104,sc105,sc106,sc107,sc108,sc109,sc110,sc111,sc112,sc113,sc114,sc115,sc116,sc117,sc118,sc119,sc120,sc121,sc122,sc123,sc124,sc125,sc126,sc127,sc128,sc129,sc130,sc131,sc132,sc133,sc134,sc135,sc136,sc137,sc138,sc139,sc140,sc141,sc142,sc143,sc144,sc145,sc146,sc147,sc148,sc149,sc150,sc151,sc152,sc153,sc154,sc155,sc156,sc157,sc158,sc159,sc160,sc161,sc162,sc163,sc164,sc165,sc166,sc167,sc168,sc169,sc170,sc171,sc172,sc173,sc174,sc175,sc176,sc177,sc178,sc179,sc180,sc181,sc182,sc183,sc184,sc185,sc186,sc187,sc188,sc189,sc190,sc191

; A partir de la tabla anterior, esta rutina modifica el código de la secuencia LD / PUSH apropiada, para que se muestre un nuevo atributo.
; Entradas: B=número de columna (0-13). C=número de scan (0-191). A=nuevo atributo (en el formato habitual del Spectrum).
; Destruye: BC,DE,HL
PokeAttr        proc   ;B=x (0-13), C=y (0-191), A=nuevo attr
                call GetDirAttr
                ld (hl),a
                ret
                endp

; A partir de la tabla anterior, esta rutina lee el operando de la secuencia LD / PUSH apropiada, y devuelve el atributo que hay allí.
; Entradas: B=número de columna (0-13). C=número de scan (0-191). Devuelve en A el atributo que hay en esas coordenadas.
; Destruye: BC,DE,HL
PeekAttr        proc
                call GetDirAttr
                ld a,(hl)
                ret
                endp

; Rutina auxiliar para las dos anteriores: calcula en HL la dirección del operando de la secuencia LD/PUSH apropiada, para proceder
; a poner un nuevo operando (atributo) o leer el que hay.
; Entradas: B=número de columna (0-13). C=número de scan (0-191). Devuelve en HL la dirección pedida.
; Destruye: BC,DE,HL
GetDirAttr      proc
                push af
                push bc
                ld b,0
                ld h,0
                ld l,c
                add hl,bc
                ld de,DirScans
                add hl,de
                ld e,(hl)
                inc hl
                ld d,(hl)   ;DE = dir. de la definición del scan
                pop bc

                push bc
                ld a,b
                sra a
                ld b,a
                ld a,6
                sub b
                sla a
                sla a
                ld l,a
                ld h,0      ;HL = (6-b/2)*4

                inc de
                inc de
                inc de      ;DE apunta a la primera secuencia de LD/PUSH
                add hl,de   ;HL apunta ahora al codop del LD correspondiente
                inc hl      ;HL apunta ahora al operando del LD (LSB)
                pop bc
                ld a,b
                and 1
                ld c,a
                ld b,0
                add hl,bc  ;HL apunta al byte menos significativo o más significativo, dependiendo de si B es par o impar
                pop af
                ret
                endp

                end Init
