; © 1998 Russian Digital Computing
; e-mail: rdc@cch.pmc.ru

; Configuration file and arguments parsing

	move.l	WB_return_message,a0
	tst.l	a0			;started from WB?
	bne.s	started_from_WB
	move.l	#rdargs_template,d1	;started from CLI
	move.l	#rdargs_array,d2
	clr.l	d3
	CALLDOS	ReadArgs
	move.l	d0,args
	bne.s	10$
	bra	err_exit
10$	_NEXTERR
	move.l	rdargs_array,a0
	tst.l	a0
	bne.s	filename_present
	clr.l	-(sp)
	bra.s	filename_absent
started_from_WB
	_NEXTERR
	move.l	sm_ArgList(a0),a1
	moveq.l	#1,d0
	cmp.l	sm_NumArgs(a0),d0
	beq.s	.one_arg_passed
	addq.l	#wa_SIZEOF,a1
.one_arg_passed
	move.l	(a1)+,d1
	move.l	(a1),-(sp)
	CALLDOS	CurrentDir
	move.l	(sp)+,a0
	tst.l	a0
	beq.s	no_dir_lock_needed
filename_present
	move.l	a0,emt36_block_ptr	;pointer to started file name
	CALLICON GetDiskObjectNew
	move.l	d0,disk_object
	beq.s	no_dir_lock_needed
	move.l	d0,a0
	move.l	do_ToolTypes(a0),-(sp)
filename_absent
	lea	tooltype_table,a2
.next_tooltype
	move.l	(a2)+,a1
	tst.l	a1
	beq.s	.end_tooltype
	clr.l	d0
	move.l	(sp),a0
	tst.l	a0			;icon present?
	beq.s	10$			;no
	move.l	a2,-(sp)
	CALLICON FindToolType
	move.l	(sp)+,a2
10$	move.l	(a2)+,a1
	move.l	(a2)+,a0
	tst.l	a0
	beq.s	20$
	move.l	(a0),d1
	beq.s	20$
	move.l	d1,d0
20$	move.l	d0,a0
	tst.l	(a2)+
	tst.l	d0
	jsr	(a1)
	tst.l	return_code
	beq.s	.next_tooltype
.end_tooltype
	tst.l	(sp)+
no_dir_lock_needed
	tst.l	return_code
	bne	err_exit
	_NEXTERR


	section	data_l

tooltype_table	dc.l	_tt_lang,_tth_lang,rdarg_language,0
		dc.l	_tt_prefs,_tth_prefs,rdarg_prefs,0
		dc.l	_tt_patch,_tth_patch,rdarg_patch,_tte_offon
		dc.l	_tt_spd,_tth_spd,rdarg_speed,_tte_spd
		dc.l	_tt_mach,_tth_mach,rdarg_machine,_tte_mach
		dc.l	_tt_snd,_tth_snd,rdarg_sound,_tte_snd
		dc.l	_tt_vid,_tth_vid,rdarg_video,_tte_vid
		dc.l	_tt_ar,_tth_ar,rdarg_auto,_tte_offon
		dc.l	_tt_port,_tth_port,rdarg_port,_tte_port
		dc.l	_tt_nofile,_tth_nofile,0,0
		dc.l	0
		dc.l	_tt_file,_tth_file,rdarg_filename,0
		dc.l	_tt_addr,_tth_addr,rdarg_address,0
		dc.l	0

	section	bss_l

rdargs_array	ds.l	1
rdarg_prefs	ds.l	1
rdarg_language	ds.l	1
rdarg_speed	ds.l	1
rdarg_machine	ds.l	1
rdarg_sound	ds.l	1
rdarg_video	ds.l	1
rdarg_auto	ds.l	1
rdarg_port	ds.l	1
rdarg_filename	ds.l	1
rdarg_address	ds.l	1
rdarg_patch	ds.l	1

	section	data_b

config_keywords
; control options
	dc.b	'SPEED',0,'SOUND',0,'VIDEO',0,'VIEW',0,'AUTOREPEAT',0
	dc.b	'PORT',0,'HARDWARE_SCROLL',0,'PAUSE_IF_INACTIVE',0
	dc.b	'SKIP_EMPTY_LOOP',0
; misc options
	dc.b	'LANGUAGE',0,'ROMPATCH',0,'JOYSTICK_MAPPING',0
	dc.b	'JOYSTICK_MODE',0,'USE_AMIRUS',0,'LATIN_KEYMAP',0
	dc.b	'CYRILLIC_KEYMAP',0,'BLOCK_KM_MODE',0,'PRINTER',0
	dc.b	'SERIAL_MODE',0,'SERIAL_DEVICE',0,'AHI',0
	dc.b	'SOFTWARE_AY',0,'KEY_CLICK',0,'SCREEN_REFRESH_PERIOD'
	dc.b	0,'SCREEN_REFRESH_DELAY',0,'SCREEN_OFF_IF_INACTIVE',0
	dc.b	'SCREEN_OFF_IF_BACKGROUND',0,'WIN_FREE_SCALE',0
	dc.b	'WIN_BORDERLESS',0,'WIN_REFRESH_PERIOD',0
	dc.b	'WIN_OFF_IF_INACTIVE',0,'WIN_X_SIZE',0
	dc.b	'WIN_Y_SIZE',0
; startup options
	dc.b	'MACHINE',0,0


	section	data_l
config_table
; control options
	dc.l	_cfg_speed
	dc.l	_cfg_sound
	dc.l	_cfg_video
	dc.l	_cfg_view
	dc.l	_cfg_autorepeat
	dc.l	_cfg_port
	dc.l	_cfg_scroll
	dc.l	_cfg_pause_if_inactive
	dc.l	_cfg_skip_empty_loop
; misc options
	dc.l	_cfg_language
	dc.l	_cfg_rompatch
	dc.l	_cfg_joystick_mapping
	dc.l	_cfg_joystick_mode
	dc.l	_cfg_use_amirus
	dc.l	_cfg_latin_keymap
	dc.l	_cfg_cyrillic_keymap
	dc.l	_cfg_block_km_mode
	dc.l	_cfg_printer
	dc.l	_cfg_serial_mode
	dc.l	_cfg_serial_device
	dc.l	_cfg_ahi
	dc.l	_cfg_ay
	dc.l	_cfg_key_click
	dc.l	_cfg_screen_refresh_period
	dc.l	_cfg_screen_refresh_delay
	dc.l	_cfg_screen_off_if_inactive
	dc.l	_cfg_screen_off_if_background
	dc.l	_cfg_window_free_scale
	dc.l	_cfg_window_borderless
	dc.l	_cfg_window_refresh_period
	dc.l	_cfg_window_off_if_inactive
	dc.l	_cfg_window_x_size
	dc.l	_cfg_window_y_size
; startup options
	dc.l	_cfg_machine

	section	sub

_cfg_speed
_cfg_sound
_cfg_view
_cfg_port
_cfg_scroll
_cfg_pause_if_inactive
_cfg_language
_cfg_joystick_mode
_cfg_use_amirus
_cfg_latin_keymap
_cfg_cyrillic_keymap
_cfg_block_km_mode
_cfg_printer
_cfg_serial_mode
_cfg_serial_device
_cfg_ahi
_cfg_ay
_cfg_key_click
_cfg_screen_refresh_period
_cfg_screen_refresh_delay
_cfg_screen_off_if_inactive
_cfg_screen_off_if_background
_cfg_window_free_scale
_cfg_window_borderless
_cfg_window_refresh_period
_cfg_window_off_if_inactive
_cfg_window_x_size
_cfg_window_y_size
	rts

	section	data_b

_tt_lang	dc.b	'LANGUAGE',0
_tt_file	dc.b	'FILENAME',0
_tt_nofile	dc.b	'NOFILE',0
_tt_prefs	dc.b	'PREFS',0
_tt_spd		dc.b	'SPEED',0
_tt_mach	dc.b	'MACHINE',0
_tt_addr	dc.b	'ADDRESS',0
_tt_snd		dc.b	'SOUND',0
_tt_vid		dc.b	'VIDEO',0
_tt_ar		dc.b	'AUTOREPEAT',0
_tt_port	dc.b	'PORT',0
_tt_patch	dc.b	'ROMPATCH',0

_tte_spd	dc.b	'1, 2, 3, 4, 5, 6, FAST',0
_tte_mach	dc.b	'10, 10.01, 11, 11M',0
_tte_snd	dc.b	'BEEPER, COVOX, AY',0
_tte_vid	dc.b	'COLOR, MONO',0
_tte_port	dc.b	'MOUSE, R/L/U/D/A/F/_',0
_tte_offon	dc.b	'ON, OFF',0

_ct_on		dc.b	'ON',0,'YES',0,'TRUE',0,'1',0,0
_ct_off		dc.b	'OFF',0,'NO',0,'FALSE',0,'0',0,0
_ct_mono	dc.b	'MONO',0,'M',0,'1',0,0
_ct_color	dc.b	'COLOR',0,'C',0,'0',0,0
_ct_snd1	dc.b	'BEEPER',0,'COVOX',0,'AY',0,0
_ct_snd2	dc.b	'0',0,'1',0,'2',0,0
_ct_mhz		dc.b	'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,0
_ct_spd		dc.b	'0',0,'F',0,'FAST',0,0
_ct_mach	dc.b	'10',0,'10.01',0,'11',0,'11M',0,0
_ct_port	dc.b	'M',0,'MOUSE',0,0

rdargs_template	dc.b	'F=File/K,Prefs/K,L=Language/K,D=Speed/K,'
		dc.b	'M=Machine/K,S=Sound/K,V=Video/K,R=AutoRepeat/K,'
		dc.b	'P=Port/K,N=FileName/K,A=Address/K,RP=ROMPatch/K',0

default_prefs	dc.b	'vbk:vbk.prefs',0

	section	sub

_tth_lang
	beq.s	read_catalog
	lea	faked_locale,a0
	move.l	d0,loc_PrefLanguages(a0)
read_catalog
	tst.l	_LocaleBase
	beq.s	.rts
	tst.l	catalog
	bne.s	.rts
	move.l	a2,-(sp)
	lea	catalog_name,a1
	lea	opencat_tags,a2
	CALLLOC	OpenCatalogA
	move.l	d0,-(sp)
	clr.l	d0
	lea	catalog_table,a2
10$	move.l	(sp),a0
	move.l	(a2),a1
	tst.l	a1
	beq.s	20$
	move.l	(a1),a1
	move.l	d0,-(sp)
	move.l	a2,-(sp)
	CALLLOC	GetCatalogStr
	move.l	(sp)+,a2
	move.l	(a2)+,a1
	move.l	d0,(a1)
	move.l	(sp)+,d0
	addq.l	#1,d0
	bra.s	10$
20$	move.l	(sp)+,catalog
	move.l	(sp)+,a2
.rts	rts

	section	bss_l

faked_locale	ds.l	12

	section	sub

_tth_nofile
	bne.s	.rts
	addq.l	#4,a2
	move.l	emt36_block_ptr,d1
	beq.s	.rts
	move.l	#MODE_OLDFILE,d2
	move.l	a2,-(sp)
	CALLDOS	Open
	move.l	d0,emt36_startup_handle
	move.l	(sp)+,a2
.rts	rts
_tth_patch
_cfg_rompatch
	beq.s	.rts
	bsr	compare_onoff
	bne	err_in_parameter
	move.b	d0,ROMpatch_flag
.rts	rts
_tth_file
	rts
_tth_prefs
	bne.s	10$
	lea	default_prefs,a0
10$	move.l	a0,d1
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open
	move.l	d0,prefs_filehandle
	beq	.endline
	clr.l	config_line
.nextline
	addq.l	#1,config_line
	move.l	prefs_filehandle,d1
	move.l	#config_string_buffer,d2
	moveq.l	#80,d3
	CALLDOS	FGets
	tst.l	d0
	beq.s	.endline
	move.l	d0,a0
	move.b	(a0),d0
	btst	#6,d0
	beq.s	.nextline	; comment
	bsr.s	find_line_end
	lea	config_keywords,a5
	move.l	a1,-(sp)
	bsr	compare_strings
	move.l	(sp)+,a0
	bne.s	.error
	bsr.s	find_line_end
	lea	config_table,a1
	move.l	-4(a1,d7.l*4),a1
	move.l	a0,d0
	jsr	(a1)
	tst.l	return_code
	beq.s	.nextline
.error	move.l	config_line,substrings
	addq.l	#1,return_code
	bra.s	.end
.endline
	clr.l	return_code
	_NEXTERR
.end	move.l	prefs_filehandle,d1
	beq.s	.rts
	CALLDOS	Close
.rts	rts
find_line_end
	move.l	a0,-(sp)
10$	move.b	(a0)+,d1
	beq.s	20$
	btst	#6,d1
	bne.s	10$
	move.l	a0,a1
20$	clr.b	-(a0)
.end	move.l	(sp)+,a0
	rts

	section	bss_l
prefs_filehandle	ds.l	1
config_string_buffer	ds.l	21
	section sub

_tth_spd
	beq.s	.rts
	lea	_ct_mhz,a5
	bsr	compare_strings
	bne.s	10$
	mulu.w	#1200,d7
	move.w	d7,speed
	addq.b	#1,delay_flag
.rts	rts
10$	lea	_ct_spd,a5
	bsr	compare_strings
	bne	err_in_parameter
	rts
_tth_mach
_cfg_machine
	beq.s	.rts
	lea	_ct_mach,a5
	bsr	compare_strings
	bne	err_in_parameter
	move.b	d7,machine_type
.rts	rts
_tth_addr
	clr.l	d0
.nexts	clr.l	d7
.nextd	move.b	(a0)+,d0	;extract address
	beq.s	.ends
	cmp.b	#"7",d0
	bhi.s	.nexts
	sub.b	#"0",d0
	bmi.s	.nexts
	asl.w	#3,d7		;next digit
	add.w	d0,d7
	bra.s	.nextd
.ends	move.w	d7,startup_address
	rts
_tth_snd
	beq.s	.rts
	lea	_ct_snd1,a5
	bsr	compare_strings
	beq.s	10$
	lea	_ct_snd2,a5
	bsr	compare_strings
	bne	err_in_parameter
10$	move.b	d7,sound_device
.rts	rts

_tth_vid
_cfg_video
	beq.s	.rts
	lea	_ct_color,a4
	lea	_ct_mono,a5
	bsr	compare_2_values
	bne	err_in_parameter
	move.w	d0,bk_color_mode_flag
.rts	rts

_cfg_skip_empty_loop
	bsr	compare_onoff
	bne	err_in_parameter
	move.b	d0,skip_SOB_flag
	rts

_tth_ar
_cfg_autorepeat
	beq.s	.rts
	bsr	compare_onoff
	bne	err_in_parameter
	move.b	d0,autorepeat_flag
.rts	rts
_tth_port
	beq.s	.rts
	lea	_ct_port,a5
	bsr	compare_strings
	bne.s	_cfg_joystick_mapping
;	mouse set - don't done now
.rts	rts
_cfg_joystick_mapping
	move.l	a0,a4
	lea	joytempstring,a3
	moveq.w	#1,d6
20$	move.b	(a4)+,(a3)
	beq.s	40$
	tst.w	d6
	beq	err_in_parameter
	cmp.b	#"_",(a3)
	beq.s	30$
	lea	joymaptext,a5
	move.l	a0,-(sp)
	move.l	a3,a0
	bsr	compare_strings
	move.l	(sp)+,a0
	bne	err_in_parameter
	asl.l	#1,d7
	move.l	d7,a1
	add.l	#joystick_mapping-2,a1
	tst.w	(a1)
	bne	err_in_parameter
	move.w	d6,(a1)
30$	asl.w	#1,d6
	bra.s	20$
40$	move.l	#5,d7
	lea	joystick_mapping,a1
50$	tst.w	(a1)+
	beq	err_in_parameter
	dbf	d7,50$
	rts

	section	data_b

; Joystick mapping for some famous games

; WayOfHell  = ldrfu
; KLADs      = rludaf
; BolderDash = __r______d_u_l
; SuperMan   = rludaf
; Digger     = rdlfu
; HOUSE      = f___l_ru(da)
; NAVVY      = rludaf
; ZOOM       = f___rd___lu

joymaptext	dc.b	'L',0,'U',0,'R',0,'D',0,'F',0,'A',0,0

	section	bss_w

joystick_mapping	ds.w	6
joytempstring		ds.w	1

	section	sub


;
; Comparison (A1) with ON & OFF values.
; Result: non-zero - error, d0=0 - OFF, d0=1 - ON
;
compare_onoff
	lea	_ct_on,a4
	lea	_ct_off,a5
;
; Comparison (A1) with two any values.
;
compare_2_values
	move.l	a4,-(sp)
	bsr.s	compare_strings
	move.l	(sp)+,a5
	bne.s	10$
	clr.l	d0
.rts	rts
10$	bsr.s	compare_strings
	bne.s	.rts
	moveq.l	#-1,d0
	clr.l	d7
	rts

compare_strings
	clr.l	d7
10$	addq.l	#1,d7
	move.l	a5,a1
	tst.b	(a1)
	bne.s	30$
	moveq.l	#-1,d7
	rts
30$	tst.b	(a5)+
	bne.s	30$
	move.l	a0,-(sp)
40$	move.b	(a0)+,d0
	bne.s	50$
	move.b	(a1)+,d0
	bra.s	70$
50$	btst	#6,d0
	beq.s	60$
	bclr	#5,d0
60$	move.b	d0,d1
	sub.b	(a1)+,d0
	beq.s	40$
70$	move.l	(sp)+,a0
	bne.s	10$
	rts

err_in_parameter
	lea	substrings,a1
	move.l	-16(a2),(a1)+
	move.l	a0,(a1)+
	move.l	-(a2),(a1)
	move.w	#20,return_code
	rts

	section	main
