extern vga_setmode
code 'bye',bye
      push word 0
      call vga_setmode
	sub ebx,ebx	;Return exit status 0
	mov eax,ebx
	inc eax		;1 = sys_exit
	int 80h
code 'key',key
	dup
	push ebx,ecx,edx
	sub ebx,ebx	;0 = stdin
	mov edx, 1	;count
	lea ecx, [esp-4]
	mov eax, 3	;sys_read
	int 80h
	mov eax, [ecx]
	pop ebx,ecx,edx
	ret
;---------------------------------------------------------------
code 'at', $at
   shl eax, 16
   call $plus
   mov dword [xy], eax
   drop
next
;---------------------------------------------------------------
;BLIT - Copy a rectangle to (x,y)
;	( w h x y -- )
code 'blit', blit
	push edx
	push edi
	call clip	;current position in ESI (temporarily in EDX)
	mov edx,edi
	call $at	;new position in EDI
	call clip
	upop ecx	;width in EAX, height in ECX
	xchg esi,edx
.1	  push ecx
	  mov ecx,eax
	  rep movsb
	  mov ecx,dword [hp]
	  sub ecx,eax
	  add esi,ecx
	  add edi,ecx
	  pop ecx
	  loop .1
	xchg esi,edx
	pop edi
	pop edx
	drop
next
;---------------------------------------------------------------
clip:
	mov edi,[xy]
	mov ecx,edi
	test cx,cx
	jns .1
	xor ecx,ecx
.1	and ecx,0xffff
	imul ecx,dword [hp]		;320*2 for 16-bit gfx
	sar edi,16
	jns .2
	xor edi,edi
.2	lea edi,[edi+ecx]	;edi*2 for 16-bit
      add edi,dword [frame]
next
;---------------------------------------------------------------
code 'cr', cr
	push ecx
        xor ecx,ecx     ;x=0
	mov cx,[xy]
	add cx,ih	;y=y+ih
	mov [xy],ecx
      add ecx,8
.scroll cmp cx,[vp]
        js .1
        pusha
        embed 'scroll'
        popa
.1      pop ecx
.2      ret
?cr:    push ecx
        mov cx,[xy+2]   ;x
        cmp cx,[hp]
        pop ecx
        js cr.2
        jmp cr
;---------------------------------------------------------------
code 'emit', emit
	push esi			; emit
	push edi			;  Store a few registers
	push ecx
	push edx
	call ?cr			;  Wrap to a new line?
.0	and eax,0xff			;  Filter byte
	imul eax,ih*iw/8		;  byte/char in FONT
	lea esi,[font+eax]		;  Load byte from FONT
	call clip			;  Clip the drawing region
	mov edx,[fore] 			;  color (2 = green)
	mov ecx,ih		;  Height of character
.line	push ecx			;  Store height
	lodsb			 	;  al=font line
	mov ecx,iw			;  Width of character
.pix	shl al,1			;  Begin drawing the character
	jnc .1
	mov [edi],dl			;  Store a pixel
.1	inc edi
	loop .pix			;  Is this line done?
	add edi,dword [hp]
      sub edi, iw			;  Yes? Then do next line
	pop ecx				;  Restore height/width
	loop .line			;  Are we done?
	pop edx				;  Yes!
	pop ecx				;  Restore the registers
	pop edi
	pop esi
	drop				; drop the character from stack
      add [xy],dword (iw<<16)		; And advance one space
next
;---------------------------------------------------------------
code 'rect', rect
	push ecx
	push edx
	push edi
	call clip
	upop ecx		  ;h
	mov edx,eax		  ;w
      mov eax,dword [fore]           ;color
.1	push ecx
        mov ecx,edx		  ;w
	rep stosb
	add edi,dword [hp]
	sub edi,edx
	pop ecx
	loop .1
	pop edi
	pop edx
	pop ecx
	drop
next
extern vga_getxdim, vga_getydim, vga_getcolors

code 'setmode',setmode
  push eax
  call vga_setmode
  add esp, byte 4

  call vga_setlinearaddressing
  call vga_getgraphmem
  mov dword [frame], eax

  call vga_getxdim
  mov [hp],eax
  shr eax,3		;Assumes 8x8 font!
  mov [hc],eax
  
  call vga_getydim
  mov [vp],eax
  shr eax,3		;Assumes 8x8 font!
  mov [vc],eax

  embed 'clear hp @ . " x " type vp @ . cr'
next
; ---------------------------------------------------
; Graphical Settings
; ---------------------------------------------------
ih equ 8                       ; Font height
iw equ 8                       ; Font width
var 'color', fore, 0           ; Foreground (text) color
var 'xy', xy, 0                ; Starting X,Y position
var 'hp', hp, 640              ; Screen Width (pixels)
var 'vp', vp, 480              ; Screen Height (pixels)
var 'hc', hc, 640/iw           ; Screen Width (characters)
var 'vc', vc, 480/ih           ; Screen Height (characters)
var 'frame', frame, 0xA0000    ; Address of video RAM

section .data
font: incbin "font"   ; Pathname to the font file
section .text
;---------------------------------------------------------------------
section .bss
tib  resb   200h
d0   resb 10000h
h0   resb 80000h
section .text
;---------------------------------------------------------------------
