;----------------------------------------------------------------------------
;File name:	PC8.S			Revision date:	1993.08.23
;Disassembler:	Ulf Ronald Andersson	Disassembled:	1993.08.23
;----------------------------------------------------------------------------
;
	include	URAn_DOS.S
	include	URAn_LA.S
;
;----------------------------------------------------------------------------
;
	section	text

start:
	move.l	4(a7),a6
	lea	stack_top(pc),a7
	move.l	bp_textlen(a6),d0
	add.l	bp_datalen(a6),d0
	add.l	bp_bss_len(a6),d0
	add.l	#1024,d0
	gemdos	Mshrink,!,(a6),d0
	tst.l	d0
	bne	exit_err		;exit on out of memory error !!!
	gemdos	Cconws,copying_font_s(pc)
	_a_init				;a0 -> line_a structure
	move.l	la_def_font(a0),a6	;a6 -> system font header
	lea	font_array(pc),a5	;a5 -> local font array
	move.l	76(a6),a4		;a4 -> system font data
	move.l	72(a6),a3		;a3 -> char offset table
	move	82(a6),d7		;d7 = system font height
	move	36(a6),d5		;d5 = lowest ascii of font
	subq	#1,d5			;d5 = lowest ascii - 1
	move	80(a6),d6		;d6 = system font width (bytes)
font_move_loop_1:
	addq	#1,d5			;d5 += 1  (stepped to next ascii)
	move	d5,d0
	mulu	d7,d0			;d0 = font_height * ascii
	lea	0(a5,d0.l),a1		;a1 -> array area for asccii's line 0,..,7
	move	d5,d0
	sub	36(a6),d0		;d0 = ascii - lowest_ascii
	lsl.w	#1,d0			;d0 = word index
	move	0(a3,d0.w),d3		;d3 = char offset
	moveq	#0,d4			;d4 = 0  (clear line#)
font_move_loop_2:
	move	d4,d0
	mulu	d6,d0			;d0 = line# * font_width = line_offs
	lea	0(a4,d0.l),a2		;a2 -> system font data of line
	move	d3,d1
	lsr.w	#3,d1			;d1 = char_offset / 8  (byte_index)
	move.b	0(a2,d1.w),d2		;\
	lsl.w	#8,d2			; > d2 = unaligned font data word
	move.b	1(a2,d1.w),d2		;/
;NB: Here is a bug, that has no effect on fonts where:  mod(char_width,8) == 0
;NB: the following line should be added:
		move	d3,d0		;d0 = char_offs
;NB: otherwise the alignment crashes when:  mod(char_width,8) != 0
	and	#7,d0			;d0 = mod(char_offs,8)
	moveq	#8,d1
	sub.b	d0,d1			;d1 = 8-mod(char_offs,8)  (8,7,..,2,1)
	lsr.w	d1,d2
	and.b	#255,d2
	move.b	d2,(a1)+		;(a1)+ = line data for char
	addq	#1,d4			;d4 += 1  (step line#)
	cmp	d7,d4			;line# = font_height ???
	bne	font_move_loop_2	;loop back for each font line
	cmp	38(a6),d5		;ascii = highest_ascii ???
	bne	font_move_loop_1	;loop back for each font character
	gemdos	Cconws,loading_MT_s(pc)
	gemdos	Pexec,#3,bbs_tos_s(pc),null_s(pc),null_s(pc)	;Load MT
	tst.l	d0
	bmi	exit_err		;exit on program load error !!!
	move.l	d0,a5			;a5 -> basepage of loaded MT
	cmpi.l	#$1817C,bp_textlen(a5)	;Program code is ok ???
	bne.s	not_MT_err		;exit on wrong program error !!!
	gemdos	Cconws,patching_s(pc)
	move.l	bp_textbeg_p(a5),a3	;a3 -> start of MT program code
	move	#$677C,$5A42(a3)
	move.l	a3,a4
	adda.l	#$150CA,a4		;a4 = a3 + $150CA
	move	#$6726,(a4)
	move	#$FF,$7496(a3)
	move	#$FF,$4C2(a3)
	lea	font_array(pc),a0
	move.l	a0,$4BC(a3)
	move	#$FF,$486(a3)
	move.l	a0,$480(a3)
	gemdos	Cconws,running_MT_s(pc)
	gemdos	Pexec,#4,(a5),(a5),(a5)		;Run MT
	move.l	d0,d7				;NB: orig ignored return value
	gemdos	Cconws,returned_MT_s		;NB: orig never told of return
	gemdos	Pterm,d7			;NB: orig never returned value
;
;
not_MT_err:					;wrong program detected !!!
	gemdos	Cconws,not_MT_s(pc)
	bra.s	exit_err			;NB: orig ran prog anyway !!!,
;						;NB: but I refuse to do it.
;
exit_err:
	gemdos	Pterm0
;
;
;----------------------------------------------------------------------------
;
	section	data
;
;
	dc.b	"Name patch start'"
bbs_tos_s:
	dc.b	'bbs.tos',NUL
	dcb.b	128-(*-bbs_tos_s),0
	dc.b	"'End of name patch",NUL
	even
;
;
copying_font_s:
	dc.b	ESC,'E'
	dc.b	'Michtron BBS 3.0 PC8 fix - Copying font'
	dc.b	CR,LF,NUL
loading_MT_s:
	dc.b	'Michtron BBS 3.0 PC8 fix - Loading Michtron'
	dc.b	CR,LF,NUL
patching_s:
	dc.b	'Michtron BBS 3.0 PC8 fix - Patching'
	dc.b	CR,LF,NUL
running_MT_s:
	dc.b	'Michtron BBS 3.0 PC8 fix - Running Michtron'
	dc.b	CR,LF,NUL
returned_MT_s:
	dc.b	'Michtron BBS 3.0 PC8 fix - Exited Michtron'
	dc.b	CR,LF,NUL
not_MT_s:
	dc.b	'Michtron BBS 3.0 PC8 fix - Not Michtron'
	dc.b	CR,LF,NUL
;
;
;----------------------------------------------------------------------------
;
	section	bss
;
font_array:	ds.b	4096
null_s:		ds.l	1
		ds.b	16384
stack_top:	ds.b	8
;
;
;----------------------------------------------------------------------------
	end	;of file:	PC8.S
