d35213935a64775454a4b5145621837a550a34a2
[ld45_start_nothing] / src / gfx.asm
1 ; vi:filetype=nasm ts=8 sts=8 sw=8:
2 ;
3 ; list of functions
4 ; -----------------
5 ; init_gfx
6 ;    initializes the video hardware and graphics routines
7 ; clear
8 ;    clears the framebuffer (not vmem)
9 ;    clobbers: ax, cx, di
10 ; swap_buffers
11 ;    copies the framebuffer to video memory
12 ;    clobbers: ax, cx, di, si
13 ; wait_vsync
14 ;    clobbers: al, dx
15 ; set_palette_entry(idx[al], r[ah], g[bl], b[bh])
16 ;    colors are 0-255
17
18 VIDMEM_SEG      equ 0a000h
19 FRAMEBUF_SEG    equ 09000h
20
21 REG_CRTC_STATUS equ 3dah
22 CRTC_VBLANK_BIT equ 08h
23
24 REG_DAC_ADDR    equ 3c8h
25 REG_DAC_DATA    equ 3c9h
26
27 init_gfx:
28         ; video mode 13h (320x200 8bpp)
29         mov ax, 13h
30         int 10h
31
32         call clear
33         ret
34
35 clear:
36         push es
37         mov ax, FRAMEBUF_SEG
38         mov es, ax
39         xor di, di
40         xor ax, ax
41         mov cx, 16000
42         rep stosd
43         pop es
44         ret
45
46 swap_buffers:
47         push ds
48         push es
49         mov ax, FRAMEBUF_SEG
50         mov ds, ax
51         xor si, si
52         mov ax, VIDMEM_SEG
53         mov es, ax
54         xor di, di
55         mov cx, 16000
56         rep movsd
57         pop es
58         pop ds
59         ret
60
61 wait_vsync:
62         mov dx, REG_CRTC_STATUS
63 .wait_vblank_end:
64         in al, dx
65         and al, CRTC_VBLANK_BIT
66         jnz .wait_vblank_end
67 .wait_vblank_start:
68         in al, dx
69         and al, CRTC_VBLANK_BIT
70         jz .wait_vblank_start
71         ret
72
73 set_palette_entry:
74         push dx
75         mov dx, REG_DAC_ADDR
76         out dx, al
77         inc dx  ; dx <- REG_DAC_DATA
78         mov al, ah
79         shr al, 2
80         out dx, al
81         mov al, bl
82         shr al, 2
83         out dx, al
84         mov al, bh
85         shr al, 2
86         out dx, al
87         pop dx
88         ret