; 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
;---------------------------------------------------------------------
hinstance rd 1
StdIn rd 1
StdOut rd 1
emit_buffer rd 1
written_buffer rd 1
; section .text
;---------------------------------------------------------------------

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

  library kernel,'KERNEL32.DLL'

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


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" 

