; vim: ft=fasm :
;---------------------------------------------------------------------
driver_init:
	invoke GetModuleHandle, 0
	mov [hinstance], eax

	invoke GetStdHandle, STD_INPUT_HANDLE
	mov [StdIn], eax
	invoke SetConsoleMode, eax, 5

	invoke GetStdHandle, STD_OUTPUT_HANDLE
	mov [StdOut], eax

	invoke SetConsoleTitle, "RetroForth"
	ret

code 'bye',bye
	invoke ExitProcess,0	; goodbye!!!

code 'cr',cr
	upsh 10
	call emit
	ret

code 'emit',emit
	upsh [no_echo]
	cmp eax, 1
	drop
	jz .a
	pusha
	mov [emit_buffer], eax	; keep it safe
	invoke WriteConsole, [StdOut], emit_buffer, 1, 0, 0
	popa
.a:	drop
	upsh 0
	upop [no_echo]
	ret

no_echo dd 0
code 'key',key
	upsh 1
	upop [no_echo]
	dup
	pusha
	invoke ReadConsole, [StdIn], emit_buffer, 1, written_buffer, 0
	popa
	mov eax, [emit_buffer]
	ret

code 'SetTitle', _SetTitle
	mov ecx,100
	push esi
	mov esi, _winstring
.a:	mov [esi], byte 0
	inc esi
	loop .a
	pop esi
	upsh _winstring
	embed 'swap 3 - cmove'
	invoke SetConsoleTitle, _winstring
next
code 'WinExec', _WinExec
	mov ecx,100
	push esi
	mov esi, _winstring
.a:	mov [esi], byte 0
	inc esi
	loop .a
	pop esi
	upsh _winstring
	embed 'swap 3 - cmove'
	invoke WinExec, _winstring, 0
next
code 'MessageBox', _MessageBox
	mov ecx,100
	push esi
	mov esi, _winstring
.a:	mov [esi], byte 0
	inc esi
	loop .a
	pop esi
	upsh _winstring
	embed 'swap 3 - cmove'
	mov ecx,100
	push esi
	mov esi, _winstring2
.b:	mov [esi], byte 0
	inc esi
	loop .b
	pop esi
	upsh _winstring2
	embed 'swap 3 - cmove'
	invoke MessageBox,0, _winstring, _winstring2, 0
next

code 'load', _read
	push eax
        invoke  CreateFile, 'blocks',GENERIC_READ,0,0,OPEN_EXISTING,0,0
	mov ebx, eax
	pop eax
        invoke  ReadFile, ebx,_filemem,1024*64,emit_buffer,0
	invoke CloseHandle, ebx
next
code 'save', _write
	push eax
        invoke  CreateFile, 'blocks',GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0
	mov ebx, eax
	pop eax
        invoke  WriteFile, ebx,_filemem,1024*64,emit_buffer,0
	invoke CloseHandle, ebx
next
code 'blocks', _mmm
  upsh _filemem
next
_filemem rb 1024*64
;---------------------------------------------------------------------
hinstance rd 1
StdIn rd 1
StdOut rd 1
emit_buffer rd 1
written_buffer rd 1
_winstring rb 100
_winstring2 rb 100
; section .text
;---------------------------------------------------------------------

; windows-specific linkage stuff:
section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',user,'USER32.DLL'

  import kernel,\
	 GetModuleHandle,'GetModuleHandleA',\
	 ExitProcess,'ExitProcess',\
	 GetStdHandle, 'GetStdHandle',\
	 WriteFile, 'WriteFile',\
	 ReadFile, 'ReadFile',\
	 ReadConsole, 'ReadConsoleA',\
	 WriteConsole, 'WriteConsoleA',\
	 SetConsoleMode, 'SetConsoleMode',\
	 SetConsoleTitle, 'SetConsoleTitleA',\
	 WinExec, 'WinExec',\
	 CreateFile, 'CreateFileA',\
	 CloseHandle,'CloseHandle'

 import user,MessageBox,'MessageBoxA'

section '.rsrc' resource data readable

  ; resource directory

  directory RT_VERSION,versions

  resource versions,\
	   1,LANG_NEUTRAL,version_info
version \
version_info,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN,LANG_ENGLISH+SUBLANG_DEFAULT,0,\
	  'FileDescription','RetroForth For Windows (windows-console)',\
	  'LegalCopyright','Released into the public domain',\
	  'FileVersion','6.24',\
	  'ProductVersion','6.24',\
	  'OriginalFilename','RF.EXE',\
	  'Credits',"We'd like to thank Ron Aaron for writing this port" 

