*
*  	File Master V1.11 / Source 210
*
*	© by    Roger Fischlin
*	        Steigerwaldweg 6
*	        6450 Hanau 7        
*	        West Germany
*
*              Phone : (06181) 650266
*
*              This program is shareware !!!
*
*
*              Use blink to link the program
*
*


	Section	"Program",CODE

	incdir   	"fh1:include/","fh1:FileMaster/"

	include	intuition/intuition.i
	include	intuition/intuition_lib.i
        	include 	exec/memory.i
	include	exec/exec_lib.i
	include	exec/interrupts.i
	include	exec/IO.i
	include	exec/execbase.i
	include	hardware/intbits.i
	include	graphics/graphics_lib.i
	include	graphics/text.i
	include	libraries/dos_lib.i
	include	libraries/dos.i
	include	devices/inputevent.i
	include	math/mathFFP_lib.i
	include	workbench/startup.i




FontFlag	set	0

SETAPEN	macro
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	\1,d0
	CALLGRAF	SetAPen
	endm

SETBPEN	macro
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	\1,d0
	CALLGRAF	SetBPen
	endm
SETDRMD	macro
 	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	\1,d0
	CALLGRAF	SetDrMd
	endm
GOTOXY	macro
 	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	\1,d0
	move.l	\2,d1
	CALLGRAF	Move
	endm

RECTFILL	macro
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	\1,d0
	move.l	\2,d1
	move.l	\3,d2
	move.l	\4,d3
	CALLGRAF	RectFill
	endm

BORDER	macro
	bra.s	MakeBox_\@
MakeBox2_\@	BOX	\1,\2,\3,\4,\5,0
MakeBox_\@	move.l	WindowPointer,a0
	move.l	wd_RPort(a0),a0
	lea.l	MakeBox2_\@(pc),a1
	moveq.l	#0,d0
	moveq.l	#0,d1
	CALLINT	DrawBorder
	endm

WRITE	macro
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a3	
	move.l	\1,d0
	move.l	\2,d1
	CALLGRAF	Move
	move.l	a3,a1	
	move.l	\3,d0
	CALLGRAF	SetAPen
	lea.l	.Write_Text\@(pc),a0
	move.l	a3,a1
	move.l	#.Write_Ende\@-.Write_Text\@,d0
	CALLGRAF	Text
	bra	.Write_Label\@
.Write_Text\@	dc.b	\4
.Write_Ende\@	dc.b	0
	even
.Write_Label\@	nop
	endm
WRITEMEM	macro
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a3	
	move.l	\1,d0
	move.l	\2,d1
	CALLGRAF	Move
	move.l	a3,a1	
	move.l	\3,d0
	CALLGRAF	SetAPen
	lea.l	\4,a0
	move.l	a3,a1
	move.l	\5,d0
	CALLGRAF	Text
	endm



BOX	macro			; macro to create a border structure
	dc.w	0,0
	dc.b	\5,0,RP_JAM1,5
	dc.l	box_\@,\6
box_\@	dc.w	\1,\2,\3,\2,\3,\4,\1,\4,\1,\2
	endm

TEXT	macro			; macro to create a text structure	
	dc.b	2,0,RP_JAM1,0
	dc.w	\1+1,\2+1
	dc.l	TOPAZ_80,T_String\@,Text2_\@
Text2_\@	dc.b	\3,0,RP_JAM1,0
	dc.w	\1,\2
	dc.l	TOPAZ_80,T_String\@,0
T_String\@	dc.b	\4,0
	even
	IFEQ	FontFlag

TOPAZ_80	dc.l	T_80name
	dc.w	TOPAZ_EIGHTY
	dc.b	FS_NORMAL,FPF_ROMFONT
	even
T_80name	dc.b	"topaz.font",0

FontFlag	set	1
	endc
	endm

GTEXT	macro			; macro to create a text structure	
	dc.b	1,4,RP_JAM2,0
	dc.w	\1,1
	dc.l	TOPAZ_80,.T_String\@,0
.T_String\@	dc.b	\2,0
	even
	endm
GTEXT2	macro			; macro to create a text structure	
	dc.b	$e,4,RP_JAM2,0
	dc.w	\1,1
	dc.l	TOPAZ_80,.T_String\@,0
.T_String\@	dc.b	\2,0
	even
	endm

GTEXT3	macro			; macro to create a text structure	
	dc.b	7,4,RP_JAM2,0
	dc.w	\1,1
	dc.l	TOPAZ_80,.T_String\@,0
.T_String\@	dc.b	\2,0
	even
	endm
TEXTR2	macro			; macro to create a text structure	
	dc.b	\4,4,RP_JAM1,0
	dc.w	\1,\2
	dc.l	TOPAZ_80,\3,0
	even

	IFEQ	FontFlag
TOPAZ_80	dc.l	T_80name
	dc.w	TOPAZ_EIGHTY
	dc.b	FS_NORMAL,FPF_ROMFONT
	even
T_80name	dc.b	"topaz.font",0

FontFlag	set	1
	endc
	endm


	clr.l	WB_Message	; startup code
	movem.l	d0/a0,-(sp)		

	jsr	OpenThem	; get libraries 

	sub.l	a1,a1	
	CALLEXEC 	FindTask
	move.l	d0,a4
	tst.l	pr_CLI(a4)
	beq.s	VonWB	
				
	movem.l	(sp)+,d0/a0
	bra	Ende_Startup

VonWB	lea.l	8(sp),sp	
	lea	pr_MsgPort(a4),a0
	CALLEXEC 	WaitPort		
	lea	pr_MsgPort(a4),a0
	CALLEXEC 	GetMsg		
	move.l	d0,WB_Message
	
	move.l	d0,a0
	move.l	sm_ArgList(a0),a0
	cmp.l	#0,a0
	beq	Ende_Startup
	move.l	(a0),d1		
	CALLDOS	CurrentDir
Ende_Startup	bsr	Start
	tst.l	WB_Message		
	beq.s	SU_Label0		
	CALLEXEC 	Forbid		
	move.l	WB_Message,a1
	CALLEXEC 	ReplyMsg
SU_Label0	moveq	#0,d0
	rts

WB_Message	dc.l	0

OpenThem	lea	dosname(pc),a1		; open libs
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_DOSBase	
	lea	intname(pc),a1                 
	moveq.l	#0,d0		
	CALLEXEC 	OpenLibrary
	move.l	d0,_IntuitionBase  
	lea	grafname(pc),a1
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_GfxBase
	rts

_DOSBase	dc.l	0
dosname	DOSNAME
_GfxBase	dc.l	0
grafname	GRAFNAME
_IntuitionBase	dc.l	0
intname	INTNAME
Window1	dc.l	0
Window2	dc.l	0
TOPAZ_80	dc.l	T_80name
	dc.w	TOPAZ_EIGHTY
	dc.b	FS_NORMAL,FPF_ROMFONT
	even
T_80name	dc.b	"topaz.font",0
	even
FONT80	dc.l	0

FontFlag	set	1

Start	
	lea.l	FONT_PAL1,a0		; prepare FONTs
	move.l	a0,d0
	add.l	#$34,d0
	move.l	d0,tf_CharData(a0)
	add.l	#($96716-$9620e),d0
	move.l	d0,tf_CharLoc(a0)

	lea.l	FONT_NTSC1,a0		
	move.l	a0,d0
	add.l	#$34,d0
	move.l	d0,tf_CharData(a0)
	move.l	tf_CharLoc(a0),d0
	sub.l	#$c4dff2,d0
	add.l	#FONT_NTSC1,d0
	move.l	d0,tf_CharLoc(a0)



	jsr	PAL_NTSC		; prepare for TV mode (PAL or NTSC)

	clr.l	Memory		; reset all 
	clr.l	AnzahlBytes
	clr.l	Cursor
	clr.l	Size
	clr.l	Filehandle
	clr.l	AnzahlBytes
	clr.b	FlagFile
	clr.b	FlagWrite
	clr.l	SearchPos
	clr.b	CompPuffer
	clr.l	SearchLength
	move.l	#1,SearchMode
	
	lea.l	TOPAZ_80,a0		; opnen TOPAZ 80
	CALLGRAF	OpenFont
	move.l	d0,FONT80

	jsr	GetPrefs		; get saved (?) preferences
	move.b	Joker,JoString+12	; copy joker to menu

	lea.l	Screendef,a0		; open screen
	CALLINT	OpenScreen
	tst.l	d0
	beq	QUIT1
	move.l	d0,ScreenPtr1


	lea.l	Windowdef1,a0		; open window	
	CALLINT	OpenWindow
	tst.l	d0
	beq	QUIT2
	move.l	d0,Window1

	move.l	d0,a0		; set colours
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	#16,d0
	lea.l	Palette,a1
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay


	sub.l	a1,a1		; DOS requester should appear in FileMaster's window
	CALLEXEC	FindTask
	move.l	d0,a0
	move.l	pr_WindowPtr(a0),OldWindow
	move.l	Window1,pr_WindowPtr(a0)

	move.b	WriteMode,d0		; set marks
	bset.b	#0,M1_I3_Sub1+mi_Flags
	bset.b	#0,M1_I3_Sub2+mi_Flags
	bset.b	#0,M1_I3_Sub3+mi_Flags
	tst.b	d0
	beq.s	.Q1
	bclr.b	#0,M1_I3_Sub1+mi_Flags
.Q1	cmp.b	#1,d0
	beq.s	.Q2
	bclr.b	#0,M1_I3_Sub2+mi_Flags
.Q2	cmp.b	#2,d0
	beq.s	.Q3
	bclr.b	#0,M1_I3_Sub3+mi_Flags
.Q3	
	

	move.l	Window1,a0		; use menus
	lea.l	Menue1,a1
	CALLINT	SetMenuStrip

	jsr	OpenCon		; open console.device
	tst.l	d0
	bne	QUIT3

	move.l	ScreenPtr1,a0
	CALLINT	ScreenToFront

* create screen

	move.w	#0,d6		; File name
	move.w	#11,d7
	move.w	#((32+1+7)*8)+8,d4
	move.w	#11+14,d5
	bsr	Areas
	
	move.w	#(32+1+7)*8+8,d6		; Sector
	move.w	#11,d7
	move.w	#((32+2+7+16+2)*8)+12,d4
	move.w	#11+14,d5
	bsr	Areas
	
	move.w	#0,d6		; Hex	
	move.w	#11+14,d7
	move.w	#(32+1+7)*8+8,d4
	move.w	Screendef+ns_Height,d5
	bsr	Areas
	
	move.w	#(32+1+7)*8+8,d6	; ASC
	move.w	#11+14,d7
	move.w	#(32+2+7+16+2)*8+12,d4
	move.w	Screendef+ns_Height,d5
	bsr	Areas


	move.w	#472+12,d6		; ABOUT
	move.w	#11,d7
	move.w	#472+84+6,d4
	move.w	#11+29,d5
	bsr	Areas
	
	move.w	#472+84+6,d6		; QUIT
	move.w	#11,d7
	move.w	#472+84+84,d4
	move.w	#11+29,d5
	bsr	Areas
	
	
	move.w	#472+12,d6		; PROJECT
	move.w	#11+29,d7
	move.w	#472+168,d4
	move.w	#11+69,d5
	bsr	Areas

	move.w	#472+12,d6		; Forward & Backward
	move.w	#11+69,d7
	move.w	#472+168,d4
	move.w	#11+89,d5
	bsr	Areas

	move.w	#472+12,d6		; Write & Undo
	move.w	#11+89,d7
	move.w	#472+168,d4
	move.w	#11+109,d5
	bsr	Areas

	move.w	#472+12,d6		; Search
	move.w	#11+109,d7
	move.w	#472+168,d4
	move.w	#11+149,d5
	bsr	Areas


	move.w	#472+12,d6		; Cursor
	move.w	#11+149,d7
	move.w	#472+84+6,d4
	move.w	#11+179,d5
	bsr	Areas
	
	move.w	#472+84+6,d6		; Mode
	move.w	#11+149,d7
	move.w	#472+84+84,d4
	move.w	#11+179,d5
	bsr	Areas

	move.w	#472+12,d6		; Space
	move.w	#11+179,d7
	move.w	#472+168,d4
	move.w	Screendef+ns_Height,d5
	bsr	Areas


	move.l	Window1,a1		; redraw gadget
	move.l	wd_FirstGadget(a1),a0
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	
	jsr	PrintMode
	jsr	INFO


WindowPointer set Window1		

NewFile	clr.l	Offset
	tst.b	FlagFile
	bne	.L1
	
	move.w	#0,d6		; Clear Screen if no file was opened
	move.w	#11,d7
	move.w	#(32+1+7)*8+8,d4
	move.w	#11+14,d5
	bsr	Areas
	
	move.w	#(32+1+7)*8+8,d6	; Sector
	move.w	#11,d7
	move.w	#(32+2+7+16+2)*8+12,d4
	move.w	#11+14,d5
	bsr	Areas
	
	move.w	#0,d6		; Hex	
	move.w	#11+14,d7
	move.w	#(32+1+7)*8+8,d4
	move.w	Screendef+ns_Height,d5
	bsr	Areas
	
	move.w	#(32+1+7)*8+8,d6	; ASC
	move.w	#11+14,d7
	move.w	#(32+2+7+16+2)*8+12,d4
	move.w	Screendef+ns_Height,d5
	bsr	Areas

	move.w	#472+12,d6		; Cursor
	move.w	#11+149,d7
	move.w	#472+84+6,d4
	move.w	#11+179,d5
	bsr	Areas
	jmp	NewMode

.L1	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	FONT80,a0
	CALLGRAF	SetFont
	SETDRMD	#RP_JAM2
	SETBPEN	#2
	WRITEMEM	#10,#11+3+6,#1,FileName,#37
	tst.l	Size
	beq	NewMode
	moveq.l	#0,d0
	jmp	GetBlock	

NewBlock	jsr	SectorNumber
	tst.l	AnzahlBytes
	beq	NewMode
	jsr	ShowPuffer
	lea.l	Color,a0
	move.l	#511,d0
.L1	move.b	#3,(a0)+
	dbra	d0,.L1

	move.l	#1,Cursor
	moveq.l	#0,d0
	jsr	SetCursor
NewPos	jsr	PrintPosition
	and.b	#1,Mode	; if HEX mode -> 1.Nibble

NewMode	jsr	PrintMode

	move.l	WindowPointer,a1	; use TOPAZ 80
	move.l	wd_RPort(a1),a1	
	move.l	FONT80,a0
	CALLGRAF	SetFont
	SETDRMD	#RP_JAM1
	

wait	move.l	Window1,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.w	im_Qualifier(a1),d3
	move.l	im_IAddress(a1),a4
	moveq.l	#0,d6
	moveq.l	#0,d7
	move.w	im_MouseX(a1),d6
	move.w	im_MouseY(a1),d7
	CALLEXEC 	ReplyMsg		; reply message
	
	moveq.l	#0,d0		
	move.b	gg_GadgetID+1(a4),d0	; get gadget ID

	cmp.l	#RAWKEY,d4
	beq	KeyPressed
	
	cmp.l	#GADGETUP,d4
	beq	GADGET
	cmp.l	#GADGETDOWN,d4
	beq	GADGET
	cmp.l	#MENUPICK,d4
	beq	MENU
	

Error	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
	bra	wait

MENU	cmp.w	#MENUNULL,d5	; test if an item was selected ?
	beq	wait
	move.l	d5,d0
	and.l	#$1f,d0
	cmp.b	#3,d0
	beq	GOTO_menu
	cmp.b	#2,d0
	beq	PROJECT_menu
	cmp.b	#1,d0
	beq	DISPLAY_menu
	tst.b	d0
	bne	wait
	move.l	d5,d0
	lsr.l	#5,d0
	and.l	#$1f,d0
	lsl.l	#2,d0
	add.l	#Menu0Table,d0
	move.l	d0,a0
	move.l	(a0),a0
	move.l	d5,d0	; d0 contains sub item
	lsr.l	#8,d0
	lsr.l	#3,d0
	and.l	#$1f,d0
	jmp	(a0)
Menu0Table	dc.l	COLOUR,NewJoker,ChangeWrite,SavePrefs,DefaultPrefs

GOTO_menu	move.l	d5,d0		; goto-menu
	lsr.l	#5,d0
	and.l	#$1f,d0
	tst.b	d0
	beq	.L1
	cmp.b	#1,d0
	beq	.L2
	jmp	BLOCK
.L1	jmp	FIRSTBLOCK
.L2	jmp	LASTBLOCK

DISPLAY_menu	move.l	d5,d0		; display-menu
	lsr.l	#5,d0
	and.l	#$1f,d0
	jmp	ChangeDisplay	

PROJECT_menu	move.l	d5,d0		; project-menu
	lsr.l	#5,d0
	and.l	#$1f,d0
	tst.b	d0
	beq.s	.W1
	cmp.b	#1,d0
	beq.s	.W2
	cmp.b	#2,d0
	beq.s	.W4
	jmp	wait
.W1	jmp	NEWCLI
.W2	jmp	NEWSHELL
.W4	jmp	MEMORY
	

	

GADGET	lsl.l	#2,d0	; get address from table
	add.l	#Vektoren,d0
	move.l	d0,a0
	move.l	(a0),a0
	jmp	(a0)
Vektoren	dc.l	OPEN,SAVE,SIZE,PREVIOUS,EXECUTE,FORWARD,BACKWARD,WRITE,UNDO,SEARCH
	dc.l	wait,ABOUT,QUIT,ChangeMode,HEX_Gadget,ASC_Gadget,BLOCK,NEXT,wait
	
QUIT	jsr	WriteQuestion
	jsr	SaveQuestion
	jsr	LEAVE		; ask
	tst.l	d0
	bne	wait
	move.l	Memory,a1		; free memory if allocated
	cmp.l	#0,a1
	beq	.CloseFile
	move.l	Size,d0
	CALLEXEC	FreeMem
	bra.s	.L1
.CloseFile	move.l	Filehandle,d1		; close file if opened
	CALLDOS	Close
.L1	jsr	CloseCon
	move.l	Window1,a0
	CALLINT	ClearMenuStrip
	
	sub.l	a1,a1
	CALLEXEC	FindTask
	move.l	d0,a0
	move.l	OldWindow,pr_WindowPtr(a0)
	move.l	Window1,a0		; bye, bye !
	CALLINT	CloseWindow
	move.l	ScreenPtr1,a0
	CALLINT	CloseScreen

NoDev	moveq.l	#0,d0
	rts

OldWindow	dc.l	0

Screendef	dc.w	0,0,640,256,3
	dc.b	0,1
	dc.w	V_HIRES	
	dc.w	CUSTOMSCREEN!SCREENBEHIND
	dc.l	TOPAZ_80
	dc.l	.S_Title,0,0
	
	even
.S_Title	dc.b	"RF-Tools : File Master   V1.11 © Roger Fischlin 10/1989            ",0
	even


Windowdef1	dc.w	0,0,640,256
	dc.b	7,1
	dc.l	GADGETUP!RAWKEY!GADGETDOWN!MENUPICK
	dc.l	ACTIVATE!SMART_REFRESH!BACKDROP!SMART_REFRESH!BORDERLESS
	dc.l	W1_G1
	dc.l	0
	dc.l	0
ScreenPtr1	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

FM_GADGET	macro
	dc.l	\1
	dc.w	\2,\3,\4,12
	dc.w	GADGHNONE!GADGIMAGE
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	.Image,0
	dc.l	.Text
	dc.l	0,0
	dc.w	\5
	dc.l	0
.Text	dc.b	0,0,RP_JAM1,0
	dc.w	(\4-((.S2-.S1-1)*8))/2,((12-8)/2)+1
	dc.l	TOPAZ_80,.S1,.Text2
.S1	dc.b	\6,0
.S2
	even
.Text2	dc.b	\7,0,RP_JAM1,0
	dc.w	(\4-((.S2-.S1-1)*8))/2,(12-8)/2
	dc.l	TOPAZ_80,.S1,0
.Image	dc.w	0,0,\4,12,3
	dc.l	0
	dc.b	0,0
	dc.l	.Image2
.Image2	dc.w	2,1,\4-4,12-2,3
	dc.l	0
	dc.b	0,\8
	dc.l	0
	endm

*
* Gadgets
*

W1_G1	FM_GADGET	W1_G2,484+6,(11+29)+5,70,0,<"Open">,1,4
W1_G2	FM_GADGET	W1_G3,484+6+3+72,(11+29)+5,70,1,<"Save As">,1,4
W1_G3	FM_GADGET	W1_G4,484+6,(11+29)+5+14+4,70,2,<"Size">,1,4
W1_G4	FM_GADGET	W1_G6,484+6+3+72,(11+29)+5+14+4,70,4,<"Execute">,1,4
W1_G6	FM_GADGET	W1_G7,484+6,(11+69)+4,70,5,<"Forward">,1,7
W1_G7	FM_GADGET	W1_G8,484+6+4+72,(11+69)+4,70,6,<"Backward">,1,7
W1_G8	FM_GADGET	W1_G9,484+6,(11+89)+4,70,7,<"Write">,1,7
W1_G9	FM_GADGET	W1_G10,484+6+3+72,(11+89)+4,70,8,<"Undo">,1,7
W1_G10	FM_GADGET	W1_G11,484+6,(11+109)+5,72+3+70,9,<"Search">,1,7
W1_G11	FM_GADGET	W1_G18,484+6,(11+109)+5+14+4,70,3,<"Previous">,1,7
W1_G18	FM_GADGET	W1_G12,484+6+3+72,(11+109)+4+14+5,70,17,<"Next">,1,7
W1_G12	FM_GADGET	W1_G13,484+9,(11+9),60,11,<"About">,1,4
W1_G13	FM_GADGET	W1_G14,484+78+9,(11+9),60,12,<"Quit">,1,4

W1_G14	dc.l	W1_G15		; mode gadget
	dc.w	472+84+6,11+149,84-6,30
	dc.w	GADGHNONE
	dc.w	GADGIMMEDIATE
	dc.w	BOOLGADGET
	dc.l	0,0
	dc.l	0
	dc.l	0,0
	dc.w	13
	dc.l	0
W1_G15	dc.l	W1_G16		; HEX gadget
	dc.w	0,11+14,8*(32+1+7)+8,256
	dc.w	GADGHNONE
	dc.w	GADGIMMEDIATE
	dc.w	BOOLGADGET
	dc.l	0,0
	dc.l	0
	dc.l	0,0
	dc.w	14
	dc.l	0
W1_G16	dc.l	W1_G17		; ASC gadget
	dc.w	8*(32+1+7)+8,11+14,8*(32+2+7+16+2)+12,256
	dc.w	GADGHNONE
	dc.w	GADGIMMEDIATE
	dc.w	BOOLGADGET
	dc.l	0,0
	dc.l	0
	dc.l	0,0
	dc.w	15
	dc.l	0
W1_G17	dc.l	0		; sector gadget
	dc.w	8*(32+1+7),11,8*(32+2+7+16+2),11+14
	dc.w	GADGHNONE
	dc.w	GADGIMMEDIATE
	dc.w	BOOLGADGET
	dc.l	0,0
	dc.l	0
	dc.l	0,0
	dc.w	16
	dc.l	0





Menue1	dc.l	Menue2
	dc.w	10,0,15*8,10,MENUENABLED
	dc.l	.MenueName
	dc.l	M1_Item1,0,0
.MenueName	dc.b	"Preferences",0
	even
M1_Item1	dc.l	M1_Item2	
	dc.w	0,0,21*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	0,.Text,0
	dc.b	"p",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"Palette",0
M1_Item2	dc.l	M1_Item3	
	dc.w	0,11,21*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	0,.Text,0
	dc.b	"j",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,JoString,0
JoString	dc.b	"Set joker : ?",0
M1_Item3	dc.l	M1_Item4
	dc.w	0,22,21*8,11,ITEMTEXT!HIGHCOMP!ITEMENABLED
	dc.l	0,.Text,0
	dc.b	0,0
	dc.l	M1_I3_Sub1,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"Write Mode",0
M1_Item4	dc.l	M1_Item5
	dc.w	0,33,21*8,11,ITEMTEXT!HIGHCOMP!ITEMENABLED!COMMSEQ
	dc.l	0,.Text,0
	dc.b	"s",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"Save prefs",0
M1_Item5	dc.l	0
	dc.w	0,44,21*8,11,ITEMTEXT!HIGHCOMP!ITEMENABLED!COMMSEQ
	dc.l	0,.Text,0
	dc.b	"d",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"Default prefs",0

M1_I3_Sub1	dc.l	M1_I3_Sub2
	dc.w	12*8,0,29*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED!CHECKIT
	dc.l	0,.Text,0
	dc.b	"a",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	24,2
	dc.l	0,.String,0
.String	dc.b	"Ask",0
M1_I3_Sub2	dc.l	M1_I3_Sub3
	dc.w	12*8,11,29*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED!CHECKIT
	dc.l	0,.Text,0
	dc.b	"w",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	24,2
	dc.l	0,.String,0
.String	dc.b	"Write without asking",0
M1_I3_Sub3	dc.l	0
	dc.w	12*8,22,29*8,11,ITEMTEXT!HIGHCOMP!ITEMENABLED!COMMSEQ!CHECKIT
	dc.l	0,.Text,0
	dc.b	"n",0
	dc.l	M1_I3_Sub1,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	24,2
	dc.l	0,.String,0
.String	dc.b	"Do not write",0




Menue2	dc.l	Menue3
	dc.w	160,0,10*8,10,MENUENABLED
	dc.l	.MenueName
	dc.l	M2_Item1,0,0
.MenueName	dc.b	"Display",0
	even
M2_Item1	dc.l	M2_Item2	
	dc.w	0,0,24*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED!CHECKIT!CHECKED
	dc.l	2+4,.Text,0
	dc.b	"1",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	24,2
	dc.l	0,.String,0
.String	dc.b	"all",0
M2_Item2	dc.l	M2_Item3	
	dc.w	0,11,24*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED!CHECKIT
	dc.l	1+4,.Text,0
	dc.b	"2",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	24,2
	dc.l	0,.String,0
.String	dc.b	"only characters",0
M2_Item3	dc.l	0
	dc.w	0,22,24*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED!CHECKIT
	dc.l	1+2,.Text,0
	dc.b	"3",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	24,2
	dc.l	0,.String,0
.String	dc.b	"only 32-125",0

Menue3	dc.l	Menue4
	dc.w	270,0,10*8,10,MENUENABLED
	dc.l	.MenueName
	dc.l	M3_Item1,0,0
.MenueName	dc.b	"Projects",0
	even
M3_Item1	dc.l	M3_Item2	
	dc.w	0,0,15*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	2+4,.Text,0
	dc.b	"i",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"NewCLI",0
M3_Item2	dc.l	M3_Item3	
	dc.w	0,11,15*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	1+4,.Text,0
	dc.b	"j",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"NewShell",0
M3_Item3	dc.l	0
	dc.w	0,22,15*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	1+2,.Text,0
	dc.b	"m",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"Memory",0

Menue4	dc.l	0
	dc.w	410,0,7*8,10,MENUENABLED
	dc.l	.MenueName
	dc.l	M4_Item1,0,0
.MenueName	dc.b	"Go to",0
	even
M4_Item1	dc.l	M4_Item2	
	dc.w	0,0,18*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	2+4,.Text,0
	dc.b	"f",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"First sector",0
M4_Item2	dc.l	M4_Item3
	dc.w	0,11,18*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	1+4,.Text,0
	dc.b	"l",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"Last sector",0
M4_Item3	dc.l	0
	dc.w	0,22,18*8,11,ITEMTEXT!COMMSEQ!HIGHCOMP!ITEMENABLED
	dc.l	1+4,.Text,0
	dc.b	"x",0
	dc.l	0,0
.Text	dc.b	4,0,RP_JAM1,0
	dc.w	4,2
	dc.l	0,.String,0
.String	dc.b	"sector",0


	




QUIT3	move.l	Window1,a0
	CALLINT	ClearMenuStrip
	move.l	Window1,a0		
	CALLINT	CloseWindow

QUIT2	move.l	ScreenPtr1,a0
	CALLINT	CloseScreen

QUIT1	move.l	#RECOVERY_ALERT,d0	; error message
	move.l	#30,d1
	lea.l	ErrorText,a0
	CALLINT	DisplayAlert

	moveq.l	#0,d0
	rts


ErrorText	dc.w	60
	dc.b	17
	dc.b	"File Master: ERROR !?! I cannot open the screen / window !"
	dc.b 	0,0



MK_Window	dc.l	0


MakeWindow	move.l	ScreenPtr1,nw_Screen(a0)	  ; open window
	CALLINT	OpenWindow		  ; draw border and background
	tst.l	d0
	beq	QUIT1
	move.l	d0,-(sp)
	move.l	d0,a0
	move.l	d0,MK_Window

WindowPointer set MK_Window

	move.w	wd_Height(a0),d5
	move.w	wd_Width(a0),d4
	subq	#1,d5
	subq	#1,d4

	move.l	wd_RPort(a0),-(sp)

	SETAPEN	#7

	move.w	d4,d2
	move.w	d5,d3
	subq.l	#2,d2
	subq.l	#1,d3
	moveq.l	#1,d1
	moveq.l	#2,d0
	move.l	(sp),a1
	CALLGRAF	RectFill
	
	SETAPEN	#0

	moveq.l	#0,d0	; Top
	moveq.l	#0,d1
	move.w	d4,d2
	moveq.l	#0,d3
	bsr	.Line
	moveq.l	#0,d0	; Bottom
	move.l	d5,d1
	move.w	d4,d2
	move.l	d5,d3
	bsr	.Line
	
	moveq.l	#0,d0	; Left I
	moveq.l	#0,d1
	moveq.l	#0,d2
	move.l	d5,d3
	bsr	.Line

	moveq.l	#1,d0	; Left II
	moveq.l	#0,d1
	moveq.l	#1,d2
	move.l	d5,d3
	bsr	.Line
	
	move.l	d4,d0	; Right I
	moveq.l	#0,d1
	move.l	d4,d2
	move.l	d5,d3
	bsr	.Line

	move.l	d4,d0	; Right II
	moveq.l	#0,d1
	move.l	d4,d2
	move.l	d5,d3
	subq.l	#1,d0
	subq.l	#1,d2
	bsr	.Line

	move.l	(sp)+,d0
	move.l	(sp),a1

	move.l	wd_FirstGadget(a1),a0
	cmp.l	#0,a0
	beq	.Label2
	sub.l	a2,a2
	CALLINT	RefreshGadgets
.Label2	move.l	(sp)+,d0

.exit	rts
	

.Line	move.l	4(sp),a1
	CALLGRAF	Move
	move.w	d2,d0
	move.w	d3,d1
	move.l	4(sp),a1
	CALLGRAF	Draw
	rts



Areas	; draw a area - used to create screen
WindowPointer set Window1
	
	subq	#1,d5
	subq	#1,d4

	move.l	Window1,a0
	move.l	wd_RPort(a0),-(sp)

	SETAPEN	#7
	
	move.w	d4,d0
	move.w	d4,d2
	move.w	d7,d1
	move.w	d5,d3	
	subq	#4,d0
	move.l	(sp),a1
	CALLGRAF	RectFill
	
	move.w	d6,d0
	move.w	d5,d1
	subq	#4,d1
	move.w	d4,d2
	move.w	d5,d3
	move.l	(sp),a1
	CALLGRAF	RectFill
	
	SETAPEN	#2

	move.w	d6,d0
	addq.l	#4,d0
	move.w	d7,d1
	addq.l	#2,d1
	move.w	d4,d2
	subq	#4,d2
	move.w	d5,d3
	subq	#2,d3
	move.l	(sp),a1
	CALLGRAF	RectFill
		
	SETAPEN	#5

	move.w	d6,d0
	move.w	d7,d1
	move.w	d7,d3
	move.w	d4,d2
	bsr	.Line

	move.w	d6,d0
	move.w	d7,d1
	move.w	d7,d3
	addq.l	#1,d1
	addq.l	#1,d3
	move.w	d4,d2
	subq	#2,d2
	bsr	.Line

	move.w	d6,d0
	move.w	d7,d1
	move.w	d6,d2
	move.w	d5,d3
	subq	#1,d3
	bsr	.Line
	
	move.w	d6,d0
	addq.l	#1,d0
	move.w	d7,d1
	move.w	d6,d2
	addq.l	#1,d2
	move.w	d5,d3
	subq	#1,d3
	bsr	.Line
	
	move.w	d6,d0
	addq	#2,d0
	move.w	d7,d1
	move.w	d6,d2
	addq	#2,d2

	move.w	d5,d3
	subq	#2,d3
	bsr	.Line

	move.w	d6,d0
	addq	#3,d0
	move.w	d7,d1
	move.w	d6,d2
	addq	#3,d2
	move.w	d5,d3
	subq	#2,d3
	bsr	.Line

	move.l	(sp)+,d0
.exit	rts
	

.Line	move.l	4(sp),a1
	CALLGRAF	Move
	move.w	d2,d0
	move.w	d3,d1
	move.l	4(sp),a1
	CALLGRAF	Draw
	rts

OpenCon	sub.l	a1,a1		; open console device
	CALLEXEC	FindTask		; it's used to convert RAWKEY
	move.l	d0,ReplyPort+$10	; into ASCII
	
	lea.l	ReplyPort,a1
	CALLEXEC	AddPort

	moveq.l	#0,d0
	moveq.l	#0,d1
	lea.l	ConName(pc),a0
	lea.l	ConIO(pc),a1
	move.l	Window1,$28(a1)
	move.l	#48,$24(a1)

	CALLEXEC	OpenDevice
	tst.l	d0
	bne	CloseCon2
	rts

ConIO	ds.b	48
ReplyPort	ds.l	8
ConName	dc.b	"console.device",0
	even


CloseCon	lea.l	ConIO(pc),a1		; close device 
	CALLEXEC	CloseDevice
CloseCon2	lea.l	ReplyPort(pc),a1
	CALLEXEC	RemPort
	moveq.l	#-1,d0
	rts

KeyPressed	cmp.b	#CURSORLEFT,d5	; cursor key ?	
	beq	MoveCursorL
	cmp.b	#CURSORRIGHT,d5
	beq	MoveCursorR
	cmp.b	#CURSORUP,d5
	beq	MoveCursorU
	cmp.b	#CURSORDOWN,d5
	beq	MoveCursorD

	btst	#7,d5
	beq	wait
	and.l	#$7f,d5
	move.l	d3,d0
	and.l	#128+64,d0	; skip if AMIGA-Key pressed 
	bne	wait	; (because user only wanted to select a menu
			; but he didn't want to enter a character) 
	cmp.b	#$45,d5	; ESC
	beq	.QUIT
	cmp.b	#$50,d5	; F1
	beq	.OPEN
	cmp.b	#$51,d5	; F2
	beq	.SAVE
	cmp.b	#$52,d5	; F3
	beq	.EXECUTE
	cmp.b	#$53,d5	; F4
	beq	.FORWARD
	cmp.b	#$54,d5	; F5
	beq	.BACKWARD
	cmp.b	#$55,d5	; F6
	beq	.WRITE
	cmp.b	#$56,d5	; F7
	beq	.UNDO
	cmp.b	#$57,d5	; F8
	beq	.SEARCH
	cmp.b	#$58,d5	; F9
	beq	.PREVIOUS
	cmp.b	#$59,d5	; F10
	beq	.NEXT
	cmp.b	#$5f,d5	; Help
	beq	.KEY_INFO
	cmp.b	#$f,d5	; NK 0
	bne	KL1
	bra.s	ChangeMode
.QUIT	jmp	QUIT
.OPEN	jmp	OPEN
.SAVE	jmp	SAVE
.EXECUTE	jmp	EXECUTE
.FORWARD	jmp	FORWARD
.BACKWARD	jmp	BACKWARD
.WRITE	jmp	WRITE
.UNDO	jmp	UNDO
.SEARCH	jmp	SEARCH
.PREVIOUS	jmp	PREVIOUS
.NEXT	jmp	NEXT
.KEY_INFO	jmp	KEY_INFO
ChangeMode	eor.b	#1,Mode	; change mode
	and.b	#1,Mode
	jmp	NewMode

KL1	tst.l	AnzahlBytes	; check, if there are bytes 
	beq	wait
	cmp.b	#$41,d5
	beq	BackSpace
	
	lea.l	IE(pc),a0	; put up INPUT EVENT structure
	move.w	d5,ie_Code(a0)
	move.w	d3,ie_Qualifier(a0)	
	move.b	#IECLASS_RAWKEY,ie_Class(a0)

	lea.l	ASCII(pc),a1	
	moveq.l	#4,d1
	sub.l	a2,a2

	move.l	ConIO+IO_DEVICE,a6	; convert INPUT EVENT into ASCII
	jsr	-$30(a6)
	cmp.b	#1,d0
	bne	wait
	tst.b	Mode
	bne	HEX_Input
	lea.l	Puffer,a0
	add.l	Cursor,a0
	move.b	ASCII,(a0)
	lea.l	Color,a0
	add.l	Cursor,a0
	move.b	#1,(a0)
	
	
	move.l	Cursor,d0
	addq.l	#1,d0
	cmp.l	AnzahlBytes,d0
	bge.s	.L2
	jsr	SetCursor
	jmp	wait
.L2	move.l	Cursor,d0
	jsr	SetCursor
	jmp	wait

	even
IE	ds.b	ie_SIZEOF
	even
ASCII	ds.b	10


WindowPointer set Window1		

ShowPuffer	SETDRMD	#RP_JAM2
	SETAPEN	#3
	SETBPEN	#2
	jsr	Set_Font

	lea.l	Puffer,a3
	move.l	a3,a4
	add.l	AnzahlBytes,a4

	moveq.l	#0,d5
	

.L1	lea.l	.Line(pc),a0
	move.w	#99,d0
.L2	move.b	#" ",(a0)+
	dbra	d0,.L2
	
	lea.l	.Line2(pc),a0
	move.w	#23,d0
.L3	move.b	#" ",(a0)+
	dbra	d0,.L3

.L4	lea.l	.Line(pc),a5
	lea.l	.Line2(pc),a6

	move.l	Bytes_Per_Row,d7
	lsr.l	#2,d7
	subq.l	#1,d7

.L5	moveq.l	#3,d6
	
.L6	moveq.l	#0,d4
	move.b	(a3)+,d4
	lea.l	HEX,a0
	
	cmp.l	a4,a3
	bhi	.L7
	
	tst.b	DisplayMode		; replace nothing (show all ASCIIs)		
	beq	.K1
	cmp.b	#1,DisplayMode		; replace 0-$1f and $80-$9f
	bne	.K2
	move.b	d4,d0
	and.l	#$7f,d0
	cmp.b	#$1f,d0
	bhi	.K1
.K4	move.b	#".",(a6)+
	bra.s	.K3

.K2	btst	#7,d4		; replace 0-$1f and $80-$ff
	bne.s	.K4
	cmp.b	#$1f,d4
	bls.s	.K4	

.K1	move.b	d4,(a6)+	
.K3	move.l	d4,d0
	lsr.b	#4,d0
	and.l	#$f,d4
	move.b	(a0,d0),(a5)+
	move.b	(a0,d4),(a5)+
	bra.s	.L8

.L7	move.b	#" ",(a5)+
	move.b	#" ",(a5)+
	move.b	#" ",(a6)+

.L8	dbra	d6,.L6

	move.b	#" ",(a5)+
	
	dbra	d7,.L5
	
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	move.l	HEX_X,d0
	move.l	d5,d1
	mulu	FONT_Y+2,d1	
	add.l	HEX_Y,d1
	CALLGRAF	Move
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	lea.l	.Line(pc),a0
	move.l	HEX_Breite,d0
	CALLGRAF	Text

	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	move.l	ASC_X,d0
	move.l	d5,d1
	mulu	FONT_Y+2,d1	
	add.l	ASC_Y,d1
	CALLGRAF	Move
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	lea.l	.Line2(pc),a0
	move.l	ASC_Breite,d0
	CALLGRAF	Text
	
	addq.l	#1,d5
	cmp.l	Zeilen,d5
	bne	.L1
	rts

.Line	ds.b	100
.Line2	ds.b	32
HEX	dc.b	"0123456789ABCDEF"
	

Set_Font	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	move.l	FM_Font,a0
	CALLGRAF	SetFont
	rts

SetCursor	tst.l	AnzahlBytes
	beq	.NoFile
	
	move.l	d0,-(sp)	; d0=Pos
	bsr.s	Set_Font
	cmp.l	#-1,Cursor
	beq	.SetNew

	lea.l	Color,a0		; clear old cursor position
	moveq.l	#0,d0
	move.l	Cursor,d1
	move.b	(a0,d1),d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetAPen
	moveq.l	#2,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetBPen
	moveq.l	#RP_JAM2,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetDrMd	
	move.l	Cursor,d0
	bsr	PutCursor	

.SetNew	lea.l	Color,a0		; pos new cursor
	moveq.l	#0,d0
	move.l	(sp),d1
	move.b	(a0,d1),d0
	move.b	d0,d5
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetAPen
	moveq.l	#4,d0
	cmp.b	#1,d5
	beq.s	.L1
	addq.l	#2,d0
.L1	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetBPen
	moveq.l	#RP_JAM2,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetDrMd	
	move.l	(sp)+,d0
	move.l	d0,Cursor
	bsr	PutCursor
.NoFile	rts

PutCursor	move.l	d0,d6
	divu	Bytes_Per_Row+2,d0
	move.l	d0,d1
	lsr.l	#8,d0
	lsr.l	#8,d0
	move.l	d0,d2
	lsr.l	#2,d2
	lsl.l	#1,d0
	add.l	d2,d0
	mulu	FONT_X+2,d0
	add.l	HEX_X,d0
	and.l	#$ff,d1
	mulu	FONT_Y+2,d1
	add.l	HEX_Y,d1

	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	Move
	
	lea.l	Puffer,a0
	add.l	d6,a0
	moveq.l	#0,d0
	move.b	(a0),d0
	lea.l	HEX,a1
	move.l	d0,d4
	lsr.b	#4,d0
	and.l	#$f,d4
	lea.l	.HEX_Puffer(pc),a0
	move.b	(a1,d0),(a0)
	move.b	(a1,d4),1(a0)
	moveq.l	#2,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	Text

	move.l	d6,d0
	divu	Bytes_Per_Row+2,d0
	move.l	d0,d1
	lsr.l	#8,d0
	lsr.l	#8,d0
	mulu	FONT_X+2,d0
	add.l	ASC_X,d0
	and.l	#$ff,d1
	mulu	FONT_Y+2,d1
	add.l	ASC_Y,d1
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	Move
	
	lea.l	Puffer,a0
	add.l	d6,a0
	moveq.l	#0,d0
	move.b	(a0),d0
	tst.b	DisplayMode		; replace nothing (show all ASCIIs)		
	beq	.K1
	cmp.b	#1,DisplayMode		; replace 0-$1f and $80-$9f
	bne	.K2
	move.b	d0,d1
	and.l	#$7f,d1
	cmp.b	#$1f,d1
	bhi	.K1
.K4	move.b	#".",d0
	bra.s	.K1
.K2	btst	#7,d0		; replace 0-$1f and $80-$ff
	bne.s	.K4
	cmp.b	#$1f,d0
	bls.s	.K4	
.K1	lea.l	.HEX_Puffer(pc),a0	
	move.b	d0,(a0)
	moveq.l	#1,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	Text
	rts
.HEX_Puffer	ds.b	2



*
* move cursor 
*

MoveCursorL	moveq.l	#0,d1
	move.w	d3,d1
	lsr.w	#1,d1
	or.w	d3,d1
	btst	#0,d1
	bne	MoveCursorL_Shift
	tst.l	Cursor
	beq.s	.wait
	move.l	Cursor,d0
	subq.l	#1,d0
	jsr	SetCursor
.wait	jmp	NewPos

MoveCursorU	moveq.l	#0,d1
	move.w	d3,d1
	lsr.w	#1,d1
	or.w	d3,d1
	btst	#0,d1
	bne	MoveCursorU_Shift
	move.l	Cursor,d0
	sub.l	ASC_Breite,d0
	tst.l	d0	
	bmi.s	.wait
	jsr	SetCursor
.wait	jmp	NewPos

MoveCursorR	moveq.l	#0,d1
	move.w	d3,d1
	lsr.w	#1,d1
	or.w	d3,d1
	btst	#0,d1
	bne	MoveCursorR_Shift
	move.l	Cursor,d0
	addq.l	#1,d0
	cmp.l	AnzahlBytes,d0
	beq.s	.wait
	jsr	SetCursor
.wait	jmp	NewPos

MoveCursorD	moveq.l	#0,d1
	move.w	d3,d1
	lsr.w	#1,d1
	or.w	d3,d1
	btst	#0,d1
	bne	MoveCursorD_Shift
	move.l	Cursor,d0
	add.l	ASC_Breite,d0
	cmp.l	AnzahlBytes,d0
	bge.s	.wait
	jsr	SetCursor
.wait	jmp	NewPos

MoveCursorL_Shift		
	move.l	Cursor,d0
	subq.l	#4,d0
	and.l	#-1-3,d0
	tst.l	d0
	bpl	.L1
	moveq.l	#0,d0
.L1	jsr	SetCursor
	jmp	NewPos

MoveCursorU_Shift	
	move.l	Cursor,d0
	moveq.l	#3,d1
.L1	sub.l	ASC_Breite,d0
	tst.l	d0	
	bmi.s	.L2
	dbra	d1,.L1
	sub.l	ASC_Breite,d0
.L2	add.l	ASC_Breite,d0
	jsr	SetCursor
.wait	jmp	NewPos

MoveCursorR_Shift	
	move.l	Cursor,d0
	addq.l	#4,d0
	and.l	#-1-3,d0
	cmp.l	AnzahlBytes,d0
	bge	.L1
	jsr	SetCursor
.wait	jmp	NewPos
.L1	move.l	AnzahlBytes,d0
	subq.l	#1,d0
	jsr	SetCursor
	jmp	NewPos
	

MoveCursorD_Shift
	move.l	Cursor,d0
	moveq.l	#3,d1
.L1	add.l	ASC_Breite,d0
	cmp.l	AnzahlBytes,d0
	bge.s	.L2
	dbra	d1,.L1
	add.l	ASC_Breite,d0
.L2	sub.l	ASC_Breite,d0
	jsr	SetCursor
.wait	jmp	NewPos

File	dc.l	0

OPEN	jsr	WriteQuestion
	jsr	SaveQuestion
	lea.l	Open_Text(pc),a5	; OPEN A FILE
	move.l	#R_G1a,R_def+nw_FirstGadget
	jsr	FileRequester
	tst.l	d0
	beq	wait
	move.l	d0,File

	move.w	#0,d6		; Clear Screen 
	move.w	#11,d7
	move.w	#((32+1+7)*8)+8,d4
	move.w	#11+14,d5
	bsr	Areas
	
	move.w	#(32+1+7)*8+8,d6	; Sector
	move.w	#11,d7
	move.w	#(32+2+7+16+2)*8+12,d4
	move.w	#11+14,d5
	bsr	Areas
	
	move.w	#0,d6		; Hex	
	move.w	#11+14,d7
	move.w	#(32+1+7)*8+8,d4
	move.w	Screendef+ns_Height,d5
	bsr	Areas
	
	move.w	#(32+1+7)*8+8,d6	; ASC
	move.w	#11+14,d7
	move.w	#(32+2+7+16+2)*8+12,d4
	move.w	Screendef+ns_Height,d5
	bsr	Areas

	move.w	#484,d6		; Cursor
	move.w	#11+149,d7
	move.w	#484+84-6,d4
	move.w	#11+179,d5
	bsr	Areas


	move.l	Memory,a1	; free memory if allocated
	cmp.l	#0,a1
	beq	.CloseFile
	move.l	Size,d0
	CALLEXEC	FreeMem
	bra.s	.L1
.CloseFile	move.l	Filehandle,d1	; close file if opened
	CALLDOS	Close
.L1	clr.l	AnzahlBytes
	clr.l	Cursor
	clr.l	Size
	clr.l	Filehandle
	clr.b	FlagFile
	clr.b	FlagWrite
	clr.l	Memory
	clr.l	SearchSector


Q	tst.b	R_Gadget
	beq	.Open_File

	move.l	File,d1		; Copy file to RAM
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	OpenError
	
	move.l	d0,Filehandle
	
	move.l	d0,d1	; get file size
	moveq.l	#0,d2
	moveq.l	#OFFSET_END,d3
	CALLDOS	Seek
	tst.l	d0
	bmi	DiskError

	move.l	Filehandle,d1	
	moveq.l	#0,d2
	moveq.l	#OFFSET_CURRENT,d3
	CALLDOS	Seek
	tst.l	d0
	bmi	DiskError
	move.l	d0,.FileSize

	move.l	Filehandle,d1	
	moveq.l	#0,d2
	moveq.l	#OFFSET_BEGINNING,d3
	CALLDOS	Seek
	tst.l	d0
	bmi	DiskError

	tst.l	.FileSize
	beq	.EmptyFile

	moveq.l	#MEMF_PUBLIC,d1
	CALLEXEC	AllocMem
	tst.l	d0
	beq	.NoMemory	
	move.l	d0,Memory
	
	move.l	d0,d2
	move.l	Filehandle,d1
	move.l	.FileSize,d3
	CALLDOS	Read
	cmp.l	.FileSize,d0
	bne	.DiskError
	
	move.l	Filehandle,d1
	CALLDOS	Close
	clr.l	Filehandle
	move.l	.FileSize,Size

.Z0	lea.l	FileName(pc),a0	; Copy file name
	move.l	#59,d0
.L0	move.b	#" ",(a0)+
	dbra	d0,.L0

	lea.l	R_File(pc),a0
	lea.l	FileName(pc),a1
.L3	move.b	(a0)+,(a1)+
	bne.s	.L3
	move.b	#" ",-1(a1)
	move.b	#1,FlagFile
	
	lea.l	FileName+28,a0		; copy file size
	move.l	Size,d0
	move.b	#"/",(a0)+
	moveq.l	#7,d2
	moveq.l	#0,d3
	lea.l	.Potenzen,a2
.Z1	move.b	#"0"-1,d1
.Z2	addq	#1,d1
	sub.l	(a2),d0
	bcc	.Z2
	add.l	(a2)+,d0
	tst.b	d2
	beq	.Z3
	cmp.b	#"0",d1
	beq	.Z4
	moveq.l	#1,d3
	bra	.Z3
.Z4	tst.b	d3
	bne	.Z3
	move.b	#" ",d1
.Z3	move.b	d1,(a0)+
	dbra	d2,.Z1
	jmp	NewFile

.Potenzen	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1

.DiskError	move.l	.FileSize,d0
	move.l	Memory,a1
	CALLEXEC	FreeMem
	clr.l	Memory
	jmp	DiskError
.NoMemory	move.l	Filehandle,d1
	CALLDOS	Close
	clr.l	Filehandle	
	jmp	NoMemory
.EmptyFile	move.l	Filehandle,d1
	CALLDOS	Close	
	clr.l	Filehandle
	jmp	EmptyFile

.FileSize	dc.l	0
.Open_File	move.l	File,d1		; Open File
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	OpenError
	move.l	d0,Filehandle
	move.l	d0,d1	
	moveq.l	#0,d2
	moveq.l	#OFFSET_END,d3
	CALLDOS	Seek
	tst.l	d0
	bmi	DiskError
	move.l	Filehandle,d1	
	moveq.l	#0,d2
	moveq.l	#OFFSET_CURRENT,d3
	CALLDOS	Seek
	tst.l	d0
	bmi	DiskError
	tst.l	d0
	beq	EmptyFile
	move.l	d0,Size
	
	bra	.Z0



FileName	dc.b	"                                                                         "
	even

Open_Text	dc.b	"               Open File:               "
	even


*
*	Disk error
*


WindowPointer set Window2

Windowdef2	dc.w	60,50,300,70
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	.Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

.Gadget1	FM_GADGET	0,125,50,50,0,<"OK">,1,4

DiskError	move.l	Filehandle,d1
	CALLDOS	Close
	clr.l	Filehandle
OpenError	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep	
	lea.l	Windowdef2,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-17*8)/2,#19,#0,<"»» DISK ERROR ««">
	WRITE	#5+(290-17*8)/2,#18,#1,<"»» DISK ERROR ««">
	SETAPEN	#0
	move.l	Window2,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a3	
	moveq.l	#6+((290-31*8)/2),d0
	move.l	#36,d1
	CALLGRAF	Move
	CALLDOS	IoErr
	bsr	.ErrorPointer
	move.l	a0,a5
	move.l	a3,a1
	move.l	#31,d0
	CALLGRAF	Text
	SETAPEN	#1
	move.l	Window2,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a3	
	moveq.l	#5+((290-31*8)/2),d0
	move.l	#35,d1
	CALLGRAF	Move
	move.l	a5,a0
	move.l	a3,a1
	move.l	#31,d0
	CALLGRAF	Text
	move.l	Window2,a0		
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait
.ErrorPointer	and.l	#$ff,d0
	tst.b	d0
	bne	.Label1
	add.b	#232,d0
.Label1	sub.b	#202,d0
	mulu	#31,d0
	add.l	#.Errors,d0
	move.l	d0,a0
	rts
.Errors	dc.b	"         Object in use         "
	dc.b	"     Object already exists     "
	dc.b	"      Directory not found      "
	dc.b	"        Object not found       "
	dc.b	"        Invalid window         "
	dc.b	"             ?????             "
	dc.b	"             ?????             "
	dc.b	" Packet requested type unknown "
	dc.b	" Invalid stream component name "
	dc.b	"     Invalid object lock       "
	dc.b	"  Object not of required type  "
	dc.b	"      Disk not validated       "
	dc.b	"     Disk write-protected      "
	dc.b	"Rename across devices attemted "
	dc.b	"     Directory not empty       "
	dc.b	"             ?????             "
	dc.b	"     Devices not mounted       "
	dc.b	"         Seek Error            "
	dc.b	"       Comment too big         "
	dc.b	"         Disk full             "
	dc.b	"File is protected from deletion"
	dc.b	"File is protected from writing "
	dc.b	"File is protected from reading "
	dc.b	"        Not a DOS disk         "
	dc.b	"       No Disk in Drive        "
	dc.b	"  No More entries in Directory " 
	dc.b	"             OK                " 


NoMemory	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep	
	lea.l	Windowdef2,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-20*8)/2,#19+10,#0,<"Not enough memory !">
	WRITE	#5+(290-20*8)/2,#18+10,#1,<"Not enough memory !">
	move.l	Window2,a0		
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait

EmptyFile	clr.l	Filehandle
	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep	
	lea.l	Windowdef2,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-16*8)/2,#19+10,#0,<"File is empty !">
	WRITE	#5+(290-16*8)/2,#18+10,#1,<"File is empty !">
	move.l	Window2,a0		
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait

*
*	File Requester 
*

	include	libraries/dosextens.i

TEXTR	macro			; macro to create a text structure	
	dc.b	\4,0,RP_JAM1,0
	dc.w	\1,\2
	dc.l	TOPAZ_80,\3,0
	even

	IFEQ	FontFlag
TOPAZ_80	dc.l	T_80name
	dc.w	TOPAZ_EIGHTY
	dc.b	FS_NORMAL,FPF_ROMFONT
	even
T_80name	dc.b	"topaz.font",0

FontFlag	set	1
	endc
	endm


R_def	dc.w	60,6,296+72,189	
	dc.b	0,1
	dc.l	GADGETUP!GADGETDOWN
	dc.l	SMART_REFRESH!ACTIVATE!RMBTRAP	
	dc.l	R_G1,0,0
R_S1	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN
R_Window	dc.l	0


R_G1a	FM_GADGET	R_G1b,10+(3*72),189-15,132,0,<"Edit On Device">,1,2
R_G1b	FM_GADGET	R_G2,10+72,189-15,132,-1,<"Edit In RAM">,1,2

R_G1c	FM_GADGET	R_G2,10+(3*72),189-15,132,0,<"ProPatch">,1,2
;R_G1d	FM_GADGET	R_G2,10+72,189-15,132,-1,<"FM-Patch">,1,2 ; someday perhaps ...

R_G1	FM_GADGET	R_G2,296-70+72,189-15,60,0,<"OK">,1,2
R_G2	FM_GADGET	R_G3,10,189-15,60,1,<"Cancel">,1,2
R_G3	FM_GADGET	R_G4,10,189-35,60,2,<"DF0:">,1,4
R_G4	FM_GADGET	R_G5,10+72,189-35,60,3,<"DF1:">,1,4
R_G5	FM_GADGET	R_G6,10+(2*72),189-35,60,4,<"RAM:">,1,4
R_G6	FM_GADGET	R_G30,10+(3*72),189-35,60,5,<"Parent">,1,4
R_G30	FM_GADGET	R_G7,10+(4*72),189-35,60,30,<"Devs">,1,4


R_G7	dc.l	R_G8
	dc.w	70,-55,216+72,10,GADGHCOMP!GRELBOTTOM,RELVERIFY,STRGADGET
	dc.l	R_G7_Box,0,R_G7_Text,0,R_G7_Info
	dc.w	0,0,0
R_G7_Box	BOX	-60,-2,216+72,9,1,R_G7_Box2
R_G7_Box2	BOX	-60,-2,-4,9,1,R_G7_Box3
R_G7_Box3	BOX	-3,-1,215+72,8,0,R_G7_Box4
R_G7_Box4	BOX	-2,-1,-1,8,0,0
R_G7_Text	TEXTR	-60+2,0,R_G7_string,1
R_G7_string	dc.b	"file  ",0
R_G7_Info	dc.l	R_File,R_File_Undo
	dc.w	0,43
	ds.w	12
R_File	ds.b	44
R_File_Undo	ds.b	44
R_G8	dc.l	R_G9
	dc.w	70,18,216+72,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	R_G7_Box,0,R_G8_Text,0,R_G8_Info
	dc.w	7,0,0
R_G8_Text	TEXTR	-60+2,0,R_G8_string,1
R_G8_string	dc.b	"Drawer",0
R_G8_Info	dc.l	R_Drawer,R_Drawer_Undo
	dc.w	0,100
	ds.w	12
R_Drawer	dc.b	":",0
	ds.b	302
R_Drawer_Undo	ds.b	302
R_G9	dc.l	R_G10
	dc.w	11,51-16,15,9,GADGHNONE!GADGIMAGE,RELVERIFY!GADGIMMEDIATE,BOOLGADGET
	dc.l	R_G9_PfeilUp,0,0,0,0
	dc.w	8,0,0
R_G10	dc.l	R_G11
	dc.w	11,51-16+63+18,15,9,GADGHNONE!GADGIMAGE,RELVERIFY!GADGIMMEDIATE,BOOLGADGET
	dc.l	R_G10_PfeilD,0,0,0,0
	dc.w	9,0,0
R_G11	dc.l	R_G12
	dc.w	11,61-16,15,52+18,GADGHNONE!GADGIMAGE,0,PROPGADGET
	dc.l	R_G11_mover,0,0,0,R_G11_Info
	dc.w	10,0,0
R_G11_mover	ds.w	6
R_G11_Info	dc.w	AUTOKNOB!PROPBORDERLESS!FREEVERT
	dc.w	0,0,0,$ffff
	ds.w	7
R_G12	dc.l	R_G13
	dc.w	27,51-16+(0*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	11,0,0
R_G13	dc.l	R_G14
	dc.w	27,51-16+(1*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	12,0,0
R_G14	dc.l	R_G15
	dc.w	27,51-16+(2*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	13,0,0
R_G15	dc.l	R_G16
	dc.w	27,51-16+(3*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	14,0,0
R_G16	dc.l	R_G17
	dc.w	27,51-16+(4*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	15,0,0
R_G17	dc.l	R_G18
	dc.w	27,51-16+(5*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	16,0,0
R_G18	dc.l	R_G19
	dc.w	27,51-16+(6*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	17,0,0
R_G19	dc.l	R_G20
	dc.w	27,51-16+(7*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	18,0,0
R_G20	dc.l	R_G21
	dc.w	27,51-16+(8*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	19,0,0
R_G21	dc.l	0
	dc.w	27,51-16+(9*9),259+72,9,GADGHCOMP,RELVERIFY,BOOLGADGET
	dc.l	0,0,0,0,0
	dc.w	20,0,0
R_G22



R_FileBox	BOX	0,0,276+72,73+18,6,R_FileBox2
R_FileBox2	BOX	0,0,16,73+18,6,R_FileBox3
R_FileBox3	BOX	0,10,16,63+18,6,0


R_maxEntries equ 300

FileRequester	cmp.l	#R_G1,R_def+nw_FirstGadget
	beq.s	R_1
	clr.w	R_G7+gg_Activation		
	
R_1	lea.l	R_def(pc),a0
	jsr	MakeWindow
	tst.l	d0
	beq	Rexit
	move.l	d0,R_Window

	move.l	d0,a1
	move.l	wd_RPort(a1),a1
	moveq.l	#0,d0
	CALLGRAF	SetAPen
	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1
	move.l	#10+17,d0
	move.l	#51-16,d1
	move.l	#11+273+1+72,d2
	move.l	#49+73+2,d3
	CALLGRAF	RectFill

	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a2	
	moveq.l	#RP_JAM1,d0	
	CALLGRAF	SetDrMd
	move.l	a2,a1
	move.l	#11,d1
	move.l	#(296+72-320)/2,d0
	CALLGRAF	Move	
	move.l	a2,a1	
	moveq.l	#0,d0
	CALLGRAF	SetAPen	
	move.l	a5,a0
	move.l	a2,a1
	move.l	#40,d0
	CALLGRAF	Text
	move.l	a2,a1	
	move.l	#10,d1
	move.l	#(296+72-320)/2,d0
	CALLGRAF	Move	
	move.l	a2,a1	
	moveq.l	#1,d0
	CALLGRAF	SetAPen	
	move.l	a5,a0
	move.l	a2,a1
	move.l	#40,d0
	CALLGRAF	Text

	move.l	a2,a1	
	moveq.l	#RP_JAM2,d0	
	CALLGRAF	SetDrMd

	move.l	R_Window,a1
	sub.l	a2,a2
	lea.l	R_G17,a0
	CALLINT	RefreshGadgets


	move.l	R_Window,a0
	move.l	wd_RPort(a0),a0
	lea.l	R_FileBox(pc),a1
	move.l	#10,d0
	move.l	#50-16,d1
	CALLINT	DrawBorder

	move.l	#-1,R_Selected

	tst.l	R_Read
	beq	R_Label30

	
R_NewDrawer	clr.l	R_Anzahl
	clr.l	R_Position
	move.l	#-1,R_Selected
	jsr	R_RemakeGadget
	jsr	R_RemakeProp
	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1
	moveq.l	#0,d0
	CALLGRAF	SetAPen
	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1
	move.l	#10+17,d0
	move.l	#51-16,d1
	move.l	#11+273+72,d2
	move.l	#49+73+2,d3
	CALLGRAF	RectFill

	move.l	#R_Drawer,d1
	moveq.l	#-2,d2
	CALLDOS	Lock
	move.l	d0,R_Lock
	tst.l	d0
	beq	R_Error
	move.l	R_Lock,d1
	move.l	#R_FIB,d2
	CALLDOS	Examine
	tst.l	d0
	beq	R_freeLock
	move.l	R_Lock,d1
	move.l	#R_FIB,d2
	CALLDOS	ExNext
	tst.l	d0
	beq	R_freeLock
	

R_Label14	lea.l	R_FIB(pc),a0
	move.l	R_Anzahl,d0
	mulu	#38,d0
	add.l	R_Namen,d0
	move.l	d0,a1
	move.l	fib_DirEntryType(a0),d0
	and.l	#1,d0
	move.b	d0,(a1)+
	move.l	#31,d0
	lea.l	fib_FileName(a0),a0
R_Label20	move.b	(a0)+,(a1)+
	dbra	d0,R_Label20
	move.b	#0,(a1)+
	move.l	fib_Size+R_FIB,(a1)+
	add.l	#1,R_Anzahl
	
	jsr	R_RemakeProp

	move.l	#1,R_Read

	move.l	R_Window,a0
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg
	tst.l	d0
	beq	R_Label40
	move.l	d0,a1
	moveq.l	#0,d2
	moveq.l	#0,d3
	move.l	im_Class(a1),d4
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC	ReplyMsg
	moveq.l	#0,d0
	move.b	gg_GadgetID+1(a4),d0
	cmp.b	#30,d0
	beq	R_Label41a
	tst.b	d0	
	bmi	R_Label41a
	cmp.b	#7,d0
	bhi	R_Label40


R_Label41a	move.l	d0,d6
	move.l	R_Lock,d1
	CALLDOS	UnLock
	move.l	d6,d0
	bra	R_Label41

R_Label40	cmp.l	#R_maxEntries,R_Anzahl
	beq	R_Label40a
	move.l	R_Lock,d1
	move.l	#R_FIB,d2
	CALLDOS	ExNext
	tst.l	d0
R_Label40a	bne	R_Label14
	move.l	#0,R_Read

R_freeLock	move.l	R_Lock,d1
	CALLDOS	UnLock
R_V3	jsr	R_SORT
	
R_Label30	jsr	R_RemakeGadget

	jsr	R_RemakeProp
	jsr	R_FilePrint

	move.l	R_def+nw_FirstGadget,a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	

R_wait	bsr	R_ChangePos

	move.l	R_Window,a0
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg
	tst.l	d0
	beq	R_wait
	move.l	d0,a1
	moveq.l	#0,d2
	moveq.l	#0,d3
	move.l	im_Class(a1),d4
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC	ReplyMsg
	moveq.l	#0,d0
	move.b	gg_GadgetID+1(a4),d0

R_Label41	cmp.b	#8,d0
	beq	R_UP
	cmp.b	#9,d0
	beq	R_DOWN
	cmp.b	#2,d0
	beq	R_DF0
	cmp.b	#3,d0
	beq	R_DF1	
	cmp.b	#30,d0
	beq	R_DEVICES
	cmp.b	#4,d0
	beq	R_RAM
	cmp.b	#7,d0
	beq	R_NewDrawer
	cmp.b	#5,d0
	beq	R_Parent
	cmp.b	#10,d0
	bgt	R_Select
	cmp.b	#1,d0
	beq	RcloseWindow
	move.b	d0,R_Gadget
R_OK	lea.l	R_Drawer(pc),a0
	lea.l	R_FileName(pc),a1
R_Label120	move.b	(a0)+,(a1)+
	bne.s	R_Label120
	tst.b	-(a1)
	cmp.b	#":",-1(a1)
	beq.s	R_Label122
	move.b	#"/",(a1)+
R_Label122	lea.l	R_File(pc),a0
R_Label121	move.b	(a0)+,(a1)+
	bne.s	R_Label121
	move.l	R_Window,A0
	CALLINT	CloseWindow
	move.l	#R_FileName,d0
	rts
	
		

RcloseWindow	move.l	R_Window,A0
	CALLINT	CloseWindow
Rexit	moveq.l	#0,d0
	rts
	dc.b	"::"
R_FileName	ds.b	400
R_Namen	dc.l	NamenPuffer
R_Lock	dc.l	0
R_Anzahl	dc.l	0	
R_Position	dc.l	0
R_Selected	dc.l	0
R_Read	dc.l	1
R_Puffer	ds.b	48*11
R_Gadget	dc.b	0
	cnop	0,4
R_FIB	ds.b	$104
R_SORT	cmp.l	#1,R_Anzahl
	bls	R_SORT_Label0	
	movem.l	d1-d7/a0-a2,-(sp)
	move.l	a0,a2

	move.l	R_Anzahl,d5
	subq.l	#1,d5

R_SORT_Label1	moveq.l	#0,d7	
	moveq.l	#0,d6	
	
R_SORT_Label2	move.l	d6,d0
	move.l	d0,d1
	addq.l	#1,d1
	bsr	R_COMPARE
	bls	R_SORT_Label3

	move.l	d6,d0
	move.l	d0,d1
	addq.l	#1,d1
	moveq.l	#1,d7
	bsr	R_SWAP

R_SORT_Label3	addq	#1,d6
	cmp.l	d6,d5
	bhi	R_SORT_Label2

	subq	#1,d5

	tst.b	d7
	bne	R_SORT_Label1	

	movem.l	(sp)+,d1-d7/a0-a2
R_SORT_Label0	rts
R_COMPARE	mulu	#38,d0
	mulu	#38,d1
	add.l	R_Namen,d0
	add.l	R_Namen,d1
	move.l	d0,a0
	move.l	d1,a1
	move.w	#38,d2
	subq.l	#1,d2
R_SORT_Label6	move.b	(a1)+,d1
	cmp.b	#"A"-1,d1
	bls	R_SORT_Label7
	cmp.b	#"Z",d1
	bhi	R_SORT_Label7
	add.b	#"a"-"A",d1
R_SORT_Label7	move.b	(a0)+,d0
	cmp.b	#"A"-1,d0
	bls	R_SORT_Label8
	cmp.b	#"Z",d0
	bhi	R_SORT_Label8
	add.b	#"a"-"A",d0
R_SORT_Label8	cmp.b	d1,d0
	bne	R_SORT_Label5
	dbra	d2,R_SORT_Label6
R_SORT_Label5	rts
R_SWAP	mulu	#38,d0
	mulu	#38,d1
	add.l	R_Namen,d0
	add.l	R_Namen,d1
	move.l	d0,a0
	move.l	d1,a1
	move.w	#38,d0
	subq.l	#1,d0
R_SORT_Label9	move.b	(a0),d1
	move.b	(a1),(a0)+
	move.b	d1,(a1)+
	dbra	d0,R_SORT_Label9
	rts
R_DF0	move.l	#"df0:",R_Drawer
	clr.b	R_Drawer+4
	lea.l	R_G8(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	bra	R_NewDrawer
R_DF1	move.l	#"df1:",R_Drawer
	clr.b	R_Drawer+4
	lea.l	R_G8(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	bra	R_NewDrawer
R_RAM	move.l	#"ram:",R_Drawer
	clr.b	R_Drawer+4
	lea.l	R_G8(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	bra	R_NewDrawer
R_Parent	lea.l	R_Drawer(pc),a0
	move.l	a0,a1
R_Label60	tst.b	(a0)+
	bne.s	R_Label60
	tst.b	-(a0)
	tst.b	-(a0)
R_Label61	cmp.l	a0,a1
	beq	R_Label62
	move.b	-(a0),d0
	cmp.b	#":",d0
	beq	R_Label63
	cmp.b	#"/",d0
	bne.s	R_Label61
	tst.b	-(a0)
R_Label63	clr.b	1(a0)
	lea.l	R_G8(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	bra	R_NewDrawer
R_Label62	bra	R_wait

R_Text	dc.b	"Drawer doesn't exist !",0 
R_Error	jsr	R_RemakeGadget
	jsr	R_RemakeProp
	jsr	R_FilePrint
	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a2	
	move.l	#58+(3*9),d1
	move.l	#68+(72/2),d0
	CALLGRAF	Move	
	move.l	a2,a1	
	moveq.l	#6,d0
	CALLGRAF	SetAPen	
	lea.l	R_Text(pc),a3
	move.l	a3,a0
	move.l	a2,a1
	moveq.l	#-1,d0
R_Label100	addq.l	#1,d0
	tst.b	(a3)+
	bne.s	R_Label100
	CALLGRAF	Text
	bra	R_wait

R_Select	sub.b	#11,d0
	and.l	#$f,d0
	add.l	R_Position,d0
	move.l	d0,d7
	mulu	#38,d0
	add.l	R_Namen,d0
	move.l	d0,a0
	move.b	(a0)+,d0
	beq	R_Dir
	cmp.b	#2,d0
	beq	R_DEV
	
	lea.l	R_File(pc),a1
	move.l	#31,d0
R_Label70	move.b	(a0)+,(a1)+
	beq.s	R_Label71
	dbra	d0,R_Label70
R_Label71	lea.l	R_G7(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	cmp.l	#R_G1,R_def+nw_FirstGadget
	bne	R_wait
	cmp.l	R_Selected,d7
	beq	R_OK	
	move.l	d7,R_Selected
	bra	R_wait
R_Dir	lea.l	R_Drawer(pc),a1
R_Label72	tst.b	(a1)+
	bne.s	R_Label72
	tst.b	-(a1)
	cmp.b	#":",-1(a1)
	beq	R_Label75
	move.b	#"/",(a1)+
R_Label75	move.l	#31,d0
R_Label73	move.b	(a0)+,(a1)+
	beq.s	R_Label74
	dbra	d0,R_Label73
R_Label74	lea.l	R_G7(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	bra	R_NewDrawer

R_DEV	lea.l	R_Drawer(pc),a1
	move.l	#31,d0
R_Label200	move.b	(a0)+,(a1)+
	beq.s	R_Label201
	dbra	d0,R_Label200
R_Label201	lea.l	R_G7(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	bra	R_NewDrawer

R_Colors	dc.b	1,6,2
	even
R_FilePrint	move.l	#48*11-1,d0
	lea.l	R_Puffer(pc),a0
R_Label1	move.b	#" ",(a0)+
	dbra	d0,R_Label1	
	move.l	R_Position,d0
	move.l	d0,d6
	moveq.l	#0,d5
	mulu	#38,d0
	add.l	R_Namen,d0
	move.l	d0,a0

R_Label2	move.l	d5,d0
	mulu	#48,d0
	add.l	#R_Puffer,d0
	move.l	d0,a1

	cmp.l	R_Anzahl,d6
	beq	R_Label6

	move.b	(a0)+,(a1)+
	moveq.l	#0,d1
	move.l	#31,d4
R_Label3	move.b	(a0)+,d0
	tst.b	d1
	bne.s	R_Label3a
	tst.b	d0
	bne.s	R_Label4
	moveq.l	#1,d1
R_Label3a	move.b	#" ",d0
R_Label4	move.b	d0,(a1)+
	dbra	d4,R_Label3
	lea.l	1(a1),a1
	lea.l	1(a0),a0	
	move.l	(a0)+,d0
	bsr	R_PrintSize
	addq.l	#1,d6
	addq.l	#1,d5
	cmp.b	#10,d5
	bne	R_Label2

R_Label6	lea.l	R_Puffer(pc),a3
	moveq.l	#0,d5
	
R_Label5	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1	
	move.l	a1,a2
	move.l	#28,d0
	move.l	d5,d1
	mulu	#9,d1	
	add.l	#58-16,d1	
	CALLGRAF	Move		
	move.l	a2,a1	
	lea.l	R_Colors(pc),a0
	and.l	#$f,d0	
	move.b	(a3)+,d0
	move.b	(a0,d0),d0
	CALLGRAF	SetAPen
	move.l	a2,a1		
	move.l	a3,a0
	move.l	#41,d0
	CALLGRAF	Text
	add.l	#47,a3
	addq	#1,d5
	cmp.b	#10,d5
	bne	R_Label5
	rts

R_PrintSize	movem.l	d0-d7/a0-a5,-(sp)
	move.b	-38(a0),d2
	tst.b	d2
	beq	R_PS_Dir
	cmp.b	#2,d2
	beq	R_PS_Dev
	moveq.l	#7,d2
	moveq.l	#0,d3
	lea.l	R_Potenzen,a2
R_PS1	move.b	#"0"-1,d1
R_PS2	addq	#1,d1
	sub.l	(a2),d0
	bcc	R_PS2
	add.l	(a2)+,d0
	tst.b	d2
	beq	R_PS3
	cmp.b	#"0",d1
	beq	R_PS4
	moveq.l	#1,d3
	bra	R_PS3
R_PS4	tst.b	d3
	bne	R_PS3
	move.b	#" ",d1
R_PS3	move.b	d1,(a1)+
	dbra	d2,R_PS1
	movem.l	(sp)+,d0-d7/a0-a5
	rts
R_Potenzen	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1
R_PS_Dir_Text	dc.b	" « Dir »"
R_PS_Dev_Text	dc.b	" « Dev »"

R_PS_Dir	lea.l	R_PS_Dir_Text(pc),a2
	moveq.l	#7,d0
R_PS_Dir2	move.b	(a2)+,(a1)+
	dbra	d0,R_PS_Dir2
	movem.l	(sp)+,d0-d7/a0-a5
	rts
R_PS_Dev	lea.l	R_PS_Dev_Text(pc),a2
	moveq.l	#7,d0
R_PS_Dev2	move.b	(a2)+,(a1)+
	dbra	d0,R_PS_Dev2
	movem.l	(sp)+,d0-d7/a0-a5
	rts
	

R_UP	tst.l	R_Position
	beq	R_wait
	bmi	R_wait
	sub.l	#1,R_Position
	bsr	R_FilePrint
	bsr	R_RemakeProp
	move.l	R_Window,a0
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg
	tst.l	d0
	beq	R_UP
	move.l	d0,a1
	CALLEXEC	ReplyMsg
	bra	R_wait
R_DOWN	move.l	R_Position,d0
	add.l	#10,d0
	cmp.l	R_Anzahl,d0
	bge	R_wait
	add.l	#1,R_Position
	bsr	R_FilePrint
	bsr	R_RemakeProp
	move.l	R_Window,a0
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg
	tst.l	d0
	beq	R_DOWN
	move.l	d0,a1
	CALLEXEC	ReplyMsg
	bra	R_wait

R_RemakeProp	lea.l	R_G11(pc),a0
	move.l	R_Window,a1
	sub.l	a2,a2
	move.l	#AUTOKNOB!PROPBORDERLESS!FREEVERT,d0
	moveq.l	#0,d1
	moveq.l	#0,d3
	move.l	R_Anzahl,d2
	sub.l	#10,d2
	tst.w	d2
	beq	R_Label10x
	bpl	R_Label10
R_Label10x	moveq.l	#1,d2
R_Label10	move.l	#$ffff,d4
	divu	d2,d4
	and.l	#$ffff,d4	
	move.l	R_Position,d2
R_Label11	mulu	d4,d2		
	CALLINT	ModifyProp
	rts
R_ChangePos	cmp.l	#10,R_Anzahl
	bls	R_Label12
	lea.l	R_G11_Info(pc),a0
	moveq.l	#0,d0
	move.w	pi_VertPot(a0),d0
	divu	pi_VertBody(a0),d0
	and.l	#$ffff,d0
	cmp.l	R_Position,d0
	beq	R_Label12
	move.l	d0,R_Position
	jsr	R_FilePrint
R_Label12	rts
R_RemakeGadget	lea.l	R_G12(pc),a0
	move.l	R_Anzahl,d0
	cmp.l	#10,d0
	bls	R_Label50
	moveq.l	#10,d0
R_Label50	tst.l	d0
	beq	R_Label51
	move.w	#GADGHCOMP,gg_Flags(a0)
	move.w	#RELVERIFY,gg_Activation(a0)
	lea.l	gg_SIZEOF(a0),a0
	subq.l	#1,d0
	bra.s	R_Label50
R_Label51	cmp.l	#R_G22,a0
	beq	R_Label52
	move.w	#GADGHNONE,gg_Flags(a0)
	move.w	#0,gg_Activation(a0)
	lea.l	gg_SIZEOF(a0),a0
	bra.s	R_Label51
R_Label52	rts

R_DEVICES	clr.l	R_Anzahl
	clr.l	R_Position
	move.l	#-1,R_Selected
	jsr	R_RemakeGadget
	jsr	R_RemakeProp
	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1
	moveq.l	#0,d0
	CALLGRAF	SetAPen
	move.l	R_Window,a1
	move.l	wd_RPort(a1),a1
	move.l	#10+17,d0
	move.l	#51-16,d1
	move.l	#11+273+1+72,d2
	move.l	#49+75,d3
	CALLGRAF	RectFill
	move.l	_DOSBase,a2
	move.l	dl_Root(a2),a2
	move.l	rn_Info(a2),a2
	add.l	a2,a2
	add.l	a2,a2
	move.l	di_DevInfo(a2),a2
	add.l	a2,a2
	add.l	a2,a2
	
R_V1	cmp.l	#DLT_DEVICE,dl_Type(a2)
	bne	R_V2
	tst.l	dl_Task(a2)
	beq	R_V2	
	move.l	R_Anzahl,d0
	mulu	#38,d0
	add.l	R_Namen,d0
	move.l	d0,a1
	move.b	#2,(a1)+
	move.l	a1,a3
	moveq.l	#0,d0
	move.l	dl_Name(a2),a0
	add.l	a0,a0
	add.l	a0,a0
	move.b	(a0)+,d0
	subq	#1,d0
R_V20	move.b	(a0)+,(a1)+
	dbra	d0,R_V20
	move.b	#":",(a1)+
	clr.b	(a1)

R_V23	add.l	#1,R_Anzahl
R_V2	move.l	(a2),a2
	add.l	a2,a2
	add.l	a2,a2
	cmp.l	#0,a2
	bne	R_V1
	bra	R_V3
	even

WindowPointer set Window1

PrintMode	SETDRMD	#RP_JAM2	; Mode ausgeben (0=HEX, 1=ASC)
	SETBPEN	#2	
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	FONT80,a0
	CALLGRAF	SetFont
	
	move.b	Mode,d0
	tst.b	d0
	beq	PM1
	WRITE	#(484+78+((78-3*8)/2)),#11+149+15-4+6,#0,<"HEX">
	rts
PM1	WRITE	#(484+78+((78-3*8)/2)),#11+149+15-4+6,#0,<"ASC">
	rts

PS_Text	dc.b	"$.../..."
	even

PrintPosition	SETDRMD	#RP_JAM2	; Cursor-Position ausgeben
	SETBPEN	#2
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	FONT80,a0
	CALLGRAF	SetFont
	
	move.l	Cursor,d0		
	lea.l	PS_Text+1(pc),a5	
	lea.l	HEX,a4
	lsr.l	#8,d0
	move.b	(a4,d0),(a5)+
	move.l	Cursor,d0
	lsr.l	#4,d0
	and.l	#$f,d0
	move.b	(a4,d0),(a5)+
	move.l	Cursor,d0
	and.l	#$f,d0
	move.b	(a4,d0),(a5)+
	
	move.l	Cursor,d0
	lea.l	1(a5),a5
	moveq.l	#2,d2
	moveq.l	#0,d3
	lea.l	PS_Potenzen,a2
.PS1	move.b	#"0"-1,d1
.PS2	addq	#1,d1
	sub.l	(a2),d0
	bcc	.PS2
	add.l	(a2)+,d0
	tst.b	d2
	beq	.PS3
	cmp.b	#"0",d1
	beq	.PS4
	moveq.l	#1,d3
	bra	.PS3
.PS4	tst.b	d3
	bne	.PS3
	move.b	#" ",d1
.PS3	move.b	d1,(a5)+
	dbra	d2,.PS1
	WRITEMEM	#484+((78-8*8)/2),#11+149+15-4+6,#0,PS_Text,#8
	rts

PS_Potenzen	dc.l	100
	dc.l	10
	dc.l	1

ASC_Gadget	sub.l	ASC_X,d6		; ASC-Feld angeklickt
	sub.l	ASC_Y,d7
	add.l	BaseLine,d7
	tst.l	d6
	bmi	.wait
	tst.l	d7
	bmi	.wait
	divu	FONT_X+2,d6
	and.l	#$ff,d6
	cmp.l	Bytes_Per_Row,d6
	bge	.wait
	divu	FONT_Y+2,d7
	and.l	#$ff,d7
	mulu	ASC_Breite+2,d7
	add.l	d6,d7
	cmp.l	AnzahlBytes,d7
	bge	.wait
	move.l	d7,d0
	jsr	SetCursor
	clr.b	Mode
	jsr	PrintPosition
	jmp	NewMode
.wait	jmp	wait

HEX_Gadget	sub.l	HEX_X,d6	; HEX-Feld angeklickt
	sub.l	HEX_Y,d7
	add.l	BaseLine,d7
	and.l	#$ffff,d7
	divu	FONT_Y+2,d7
	and.l	#$ffff,d7
	mulu	Bytes_Per_Row+2,d7
	
	divu	FONT_X+2,d6
	and.l	#$ffff,d6	
	divu	#9,d6

	move.l	d6,d5
	and.l	#$ffff,d6
	lsl.w	#2,d6
	
	swap	d5
	and.l	#$ffff,d5
	cmp.b	#8,d5
	beq	.wait
	lsr.l	#1,d5
	add.l	d5,d6
	
	add.l	d6,d7
	cmp.l	AnzahlBytes,d7
	bge	.wait
	move.l	d7,d0
	bsr	SetCursor
	move.b	#1,Mode
	jsr	PrintPosition
	jmp	NewMode
.wait	jmp	wait

HEX_Input	moveq.l	#0,d0	
	move.b	ASCII,d0
	lea.l	.Hex(pc),a0	; ASCII in d0 zu Nibble in D1 umsetzen
	moveq.l	#-1,d1
.L1	addq.l	#1,d1
	cmp.b	#32,d1
	bhi	.Error
	cmp.b	(a0)+,d0
	bne.s	.L1
	and.l	#$f,d1	; egal ob Klein- oder Großbuchstabe			
	
	cmp.b	#1,Mode	; erstes Nibble ?	
	beq	.L2
	or.b	Nibble,d1
	lea.l	Puffer,a0
	add.l	Cursor,a0
	move.b	d1,(a0)
	lea.l	Color,a0
	add.l	Cursor,a0
	move.b	#1,(a0)
	move.b	#1,Mode
	move.l	Cursor,d0
	addq.l	#1,d0
	cmp.l	AnzahlBytes,d0
	bge.s	.L3
	jsr	SetCursor
	jmp	wait
.L3	move.l	Cursor,d0
	jsr	SetCursor
	jmp	wait

.L2	lea.l	.Hex+16(pc),a0
	move.b	(a0,d1),.ASCII
	lsl.b	#4,d1	
	move.b	d1,Nibble
	move.b	#3,Mode

	bsr	Set_Font	; erstes Nibble ausgeben
	
	lea.l	Color,a0		
	move.l	Cursor,d1
	move.b	(a0,d1),d1
	moveq.l	#4,d0
	moveq.l	#6,d4
	cmp.b	#1,d1
	beq.s	.L5
	addq.l	#2,d0
	subq.l	#2,d4
.L5	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetBPen
	move.l	d4,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetAPen
	

	moveq.l	#RP_JAM2,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1	
	CALLGRAF	SetDrMd	

	move.l	Cursor,d0
	divu	Bytes_Per_Row+2,d0
	move.l	d0,d1
	lsr.l	#8,d0
	lsr.l	#8,d0
	move.l	d0,d2
	lsr.l	#2,d2
	lsl.l	#1,d0
	add.l	d2,d0
	mulu	FONT_X+2,d0
	add.l	HEX_X,d0
	and.l	#$ff,d1
	mulu	FONT_Y+2,d1
	add.l	HEX_Y,d1

	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	Move
	
	moveq.l	#1,d0
	move.l	Window1,a1
	move.l	wd_RPort(a1),a1
	lea.l	.ASCII(pc),a0
	CALLGRAF	Text
	jmp	wait

.Hex	dc.b	"0123456789abcdef"	; Hex in Groß- und
	dc.b	"0123456789ABCDEF"	; Kleinbuchstaben
.ASCII	dc.b	0
	even
.Error	jmp	Error		; Bildschirm aufblinken lassen

BackSpace	cmp.b	#3,Mode
	bne.s	.wait
	move.l	Cursor,d0
	jsr	SetCursor
	move.b	#1,Mode
.wait	jmp	wait



GetBlock	move.l	d0,.Offset
	tst.l	Memory		; get block offset d0
	beq	.GetBlockDisk
	move.l	Memory,a0
	add.l	d0,a0
	lea.l	Puffer,a1
	lea.l	UndoPuffer,a2
	move.l	#511,d0
.L0	move.b	(a0),(a1)+	
	move.b	(a0)+,(a2)+	
	dbra	d0,.L0
	move.l	Size,d0
	sub.l	.Offset,d0
	cmp.l	#512,d0
	bls	.L3
	move.l	#512,d0
.L3	move.l	d0,AnzahlBytes
	move.l	.Offset,Offset
	jmp	NewBlock

.GetBlockDisk	move.l	Filehandle,d1
	tst.l	d1
	beq	.Error
	move.l	.Offset,d2
	moveq.l	#OFFSET_BEGINNING,d3
	CALLDOS	Seek
	move.l	Filehandle,d1
	move.l	Size,d3
	sub.l	.Offset,d3
	cmp.l	#512,d3
	bls	.L1
	move.l	#512,d3
.L1	move.l	d3,d5
	move.l	#Puffer2,d2
	CALLDOS	Read
	cmp.l	d0,d5
	bne	OpenError
	lea.l	Puffer2,a0
	lea.l	Puffer,a1
	lea.l	UndoPuffer,a2
	move.l	#511,d0
.L2	move.b	(a0),(a1)+	
	move.b	(a0)+,(a2)+	
	dbra	d0,.L2
	move.l	d5,AnzahlBytes
	move.l	.Offset,Offset
.Error	jmp	NewBlock			
.Offset	dc.l	0

WindowPointer set Window1
SectorNumber	move.l	Size,d0	; show current sector number and number of sectors
	divu	#512,d0
	move.l	d0,d1
	swap	d1
	and.l	#$ffff,d0
	tst.w	d1
	beq.s	.SN1
	addq	#1,d0
.SN1	lea.l	SN_Text+12(pc),a0
	bsr	SN_Number
	move.l	Offset,d0
	add.l	#511,d0
	divu	#512,d0
	move.l	d0,d1
	swap	d1
	and.l	#$ffff,d0
	tst.w	d1
	beq.s	.SN2
	addq	#1,d0
.SN2	lea.l	SN_Text+5(pc),a0
	bsr	SN_Number
	SETBPEN	#2
	SETDRMD	#RP_JAM2
	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	FONT80,a0
	CALLGRAF	SetFont
	WRITEMEM	#(32+1+7+1)*8+8,#11+3+6,#1,SN_Text,#17
	rts
SN_Number	moveq.l	#4,d2
	moveq.l	#0,d3
	lea.l	SN_Potenzen,a2
SN_Z1	move.b	#"0"-1,d1
SN_Z2	addq	#1,d1
	sub.l	(a2),d0
	bcc	SN_Z2
	add.l	(a2)+,d0
	tst.b	d2
	beq	SN_Z3
	cmp.b	#"0",d1
	beq	SN_Z4
	moveq.l	#1,d3
	bra	SN_Z3
SN_Z4	tst.b	d3
	bne	SN_Z3
	move.b	#" ",d1
SN_Z3	move.b	d1,(a0)+
	dbra	d2,SN_Z1
	rts
SN_Potenzen	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1	


SN_Text	dc.b	"Sec :..... /....."

FORWARD	tst.b	FlagFile		; next sector
	beq	.Nothing
	move.l	Offset,d0
	add.l	#512,d0
	cmp.l	Size,d0
	bge	.Nothing
	jsr	WriteQuestion
	move.l	Offset,d0
	add.l	#512,d0
	jmp	GetBlock
.Nothing	jmp	wait

BACKWARD	tst.b	FlagFile		; previous sector
	beq	.Nothing
	move.l	Offset,d0
	sub.l	#512,d0
	tst.l	d0
	bmi	.Nothing
	jsr	WriteQuestion
	move.l	Offset,d0
	sub.l	#512,d0
	jmp	GetBlock
.Nothing	jmp	wait

UNDO	tst.b	FlagFile		; next sector
	beq	.Nothing
	lea.l	UndoPuffer,a0
	lea.l	Puffer,a1
	move.l	#511,d0
.L1	move.b	(a0)+,(a1)+
	dbra	d0,.L1
	jmp	NewBlock
.Nothing	jmp	wait


*
* write sector 
*

WRITE	jsr	SaveBlock
	jsr	ShowPuffer
	lea.l	Color,a0
	move.l	#511,d0
.L1	move.b	#3,(a0)+
	dbra	d0,.L1
	move.l	Cursor,d0
	jsr	SetCursor
	jmp	NewPos

SaveBlock	tst.l	Memory		; Save current block
	beq	.SaveBlockDisk		; Subroutine
	move.l	Memory,a0
	add.l	Offset,a0
	lea.l	Puffer,a1
	lea.l	UndoPuffer,a2
	move.l	#511,d0
.L0	move.b	(a1),(a0)+	
	move.b	(a1)+,(a2)+	
	dbra	d0,.L0
	move.b	#1,FlagWrite
	rts

.SaveBlockDisk	move.l	Filehandle,d1		; write block to device
	tst.l	d1
	beq	.Error
	move.l	Offset,d2
	moveq.l	#OFFSET_BEGINNING,d3
	CALLDOS	Seek
	move.l	Filehandle,d1
	move.l	#Puffer,d2
	move.l	AnzahlBytes,d3
	CALLDOS	Write
	cmp.l	AnzahlBytes,d0
	bne	.DiskError
	lea.l	Puffer,a1
	lea.l	UndoPuffer,a2
	move.l	#511,d0
.L2	move.b	(a1)+,(a2)+	
	dbra	d0,.L2
.Error	rts
.DiskError	lea.l	4(sp),sp	; Leave subroutine with returning
	jmp	OpenError	
	

*
* SAVE AS - you mustn't edit on device
*

SAVE_Text	dc.b	"               Save File:               "
	even
SAVE	tst.b	FlagFile
	beq	.wait
	tst.l	Memory	
	beq	RAM_ONLY
	jsr	WriteQuestion
	lea.l	SAVE_Text(pc),a5	; Save file
	move.l	#R_G1,R_def+nw_FirstGadget
	jsr	FileRequester
	tst.l	d0
	beq	.wait
	move.l	d0,.File
	move.l	.File,d1		; Copy file from RAM to device
	move.l	#MODE_NEWFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	OpenError
	move.l	d0,Filehandle
	move.l	d0,d1
	move.l	Size,d3
	move.l	Memory,d2
	CALLDOS	Write
	cmp.l	Size,d0
	bne	DiskError
	
	move.l	Filehandle,d1
	CALLDOS	Close
	clr.b	FlagWrite
.wait	jmp	wait
.File	dc.l	0

*
* error message - "you mustn't edit on device !"
*

WindowPointer set Window2

RAM_ONLY	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep	
	lea.l	Windowdef2,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-28*8)/2,#19+10,#0,<"You mustn't edit on device !">
	WRITE	#5+(290-28*8)/2,#18+10,#1,<"You mustn't edit on device !">
	move.l	Window2,a0		
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait


*
* question "Do ya really wanna quit ?"
*


WindowPointer set Window2

Windowdef3	dc.w	60,50,300,70
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	LE_Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

LE_Gadget1	FM_GADGET	LE_Gadget2,50,50,50,1,<"NO">,1,4
LE_Gadget2	FM_GADGET	0,200,50,50,0,<"YES">,1,4


LEAVE	lea.l	Windowdef3,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-28*8)/2,#19+10,#0,<"Do you really want to quit ?">
	WRITE	#5+(290-28*8)/2,#18+10,#1,<"Do you really want to quit ?">
	


	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
.wait	rts

*
* Question "Write sector ?" & write sector
*

WriteQuestion	move.l	(sp)+,.Return
	lea.l	Color,a0	; check, if you've changed anything
	move.l	#511,d0
.A1	cmp.b	#1,(a0)+
	beq.s	.A2
	dbra	d0,.A1
	bra	.NoWrite
.A2	cmp.b	#1,WriteMode	; which mode ?
	beq	.Autowrite
	cmp.b	#2,WriteMode
	beq	.NoWrite
	lea.l	Windowdef3,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.NoWrite
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-24*8)/2,#19,#0,<"Do you want to write the">
	WRITE	#6+(290-8*8)/2,#19+15,#0,<"sector ?">
	WRITE	#5+(290-24*8)/2,#18,#1,<"Do you want to write the">
	WRITE	#5+(290-8*8)/2,#18+15,#1,<"sector ?">


	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
	tst.b	d0
	bne	.NoWrite
.Autowrite	jsr	SaveBlock		; write sector
	jsr	ShowPuffer
	lea.l	Color,a0
	move.l	#511,d0
.L1	move.b	#3,(a0)+
	dbra	d0,.L1
.NoWrite	move.l	.Return,a0
	jmp	(a0)
.Return	dc.l	0


*	
*	change colours
*
* the basic palette program was taken from XColor 1.2 . The public
* domain program of mine was published on FISH 244. 
*
*	

_MathBase	dc.l	0
Mathname	FFPNAME

COLOUR	move.w	#$ff,Colour_Mode
	lea	Mathname(pc),a1
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_MathBase
	bne	.C1
.C0	jmp	wait
.C1	clr.b	Colour_Mode		; set to normal mode
	move.b	#$ff,OldColour_Mode

	lea.l	NewWindow1,a0
	jsr	MakeWindow		; open the window
	tst.l	d0
	beq	.C0
	move.l	d0,Window1_Ptr

	move.l	d0,a1	
	move.l	wd_RPort(a1),a1
	moveq.l	#7,d0
	CALLGRAF	SetBPen

	jsr	GetDepth
	jsr	SaveForUndo		; save color for undo function
IN3	
IN1	move.l	Window1_Ptr,a1		; draw border
	move.l	wd_RPort(a1),a0
	lea.l	CG_Box(pc),a1
	move.w	#10,d0
	move.w	#15,d1
	CALLINT	DrawBorder

IN0	lea.l	Colour_Gadget1,a0		; refresh gadgets
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets

	clr.l	COLOR		; current color = 0	
	jsr	GetDepth		; get depth
	jsr	MakeCG		; create color gadget
IN2	jsr	NewColor		; edit new color
	jsr	RGB		; write R,G,B



Colour_wait	lea.l	Colour_Gadget1,a0		; refresh gadgets again
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets

	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a2
	moveq.l	#1,d0
	CALLGRAF	SetAPen

	move.l	a2,a1	
	move.w	#(185-13*8)/2,d0
	move.w	#10,d1
	CALLGRAF	Move	


	moveq.l	#0,d0
	move.b	Colour_Mode,d0
	cmp.b	OldColour_Mode,d0
	beq	Colour_wait2
	move.b	d0,OldColour_Mode
	mulu	#14,d0
	add.l	#Title0,d0
	move.l	d0,a0
	move.l	#13,d0
	move.l	a2,a1
	CALLGRAF	Text

Colour_wait2	move.l	Window1_Ptr,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	move.w	im_MouseX(a1),d6
	move.w	im_MouseY(a1),d7
	CALLEXEC 	ReplyMsg		; reply message		
	cmp.l	#GADGETDOWN,d4
	beq	G1_HandlerA
	
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
	tst.w	d0
	beq	G0_Handler
	cmp.w	#3,d0
	bls	G1_HandlerB
	cmp.w	#7,d0	
	beq	UNDO_colour
	cmp.b	#10,d0
	beq	Black_White
	cmp.b	#5,d0
	beq	UNDO_ALL
	cmp.b	#11,d0
	beq	ANTIK
	cmp.b	#4,d0
	beq	QUIT_color
	cmp.b	#12,d0
	beq	COPY
	cmp.b	#13,d0
	beq	EXCHANGE
	cmp.b	#14,d0
	beq	SPREAD
	cmp.b	#20,d0
	beq	DEFAULT
	

	bra	Colour_wait	


QUIT_color	move.l	Window1_Ptr,a0		; the exit
	CALLINT	CloseWindow
	move.l	_MathBase,a1
	CALLEXEC	CloseLibrary
	jmp	wait




MakeCG	move.l	Depth,d0		; get size of block
	subq	#1,d0		; from table and draw
	lsl.l	#2,d0		; the blocks
	lea.l	SizeTable,a0
	move.w	(a0,d0),d6
	move.w	2(a0,d0),d7
	moveq.l	#0,d5
	move.w	#0,-(sp)


MCG0	moveq.l	#0,d4

MCG1	move.w	(sp),d0
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a3
	CALLGRAF	SetAPen
	move.l	a3,a1
	move.w	d4,d0
	move.w	d5,d1
	add.w	d6,d4
	move.w	d4,d2
	move.w	d5,d3
	add.w	d7,d3
	add	#10,d0
	add	#15,d1
	add	#10,d2
	add	#15,d3
	CALLGRAF	RectFill
	add.w	#1,(sp)
	cmp.w	#159,d4
	bls	MCG1
MCG2	add.w	d7,d5
	cmp.w	#39,d5
	bls	MCG0
	lea.l	2(sp),sp
	rts	
	
Depth	dc.l	0
SizeTable	dc.w	160/2,40/1	; 2   colors
	dc.w	160/2,40/2	; 4   colors
	dc.w	160/4,40/2	; 8   colors
	dc.w	160/4,40/4	; 16  colors
	dc.w	160/8,40/4	; 32  colors
	dc.w	160/16,40/4	; 64  colors	
	
	
Colour_Gadget0	dc.l	Colour_Gadget10
	dc.w	10,15,160,40
	dc.w	GADGHNONE
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	0,0
	dc.l	0
	dc.l	0,0
	dc.w	0
	dc.l	0

Colour_Gadget1	dc.l	Colour_Gadget2
	dc.w	40+10,50+15,114,11
	dc.w	GADGHCOMP
	dc.w	RELVERIFY!GADGIMMEDIATE
	dc.w	PROPGADGET
	dc.l	G1_image,0
	dc.l	0
	 dc.l	0,G1_info
	dc.w	1
	dc.l	0
G1_image	ds.w	4
G1_info	dc.w	FREEHORIZ!AUTOKNOB
	dc.w	0
	dc.w	0
	dc.w	$ffff/15
	ds.w	7
Colour_Gadget2	dc.l	Colour_Gadget3
	dc.w	40+10,50+30,114,11
	dc.w	GADGHCOMP
	dc.w	RELVERIFY!GADGIMMEDIATE
	dc.w	PROPGADGET
	dc.l	G2_image,0
	dc.l	0
	dc.l	0,G2_info
	dc.w	2
	dc.l	0
G2_image	ds.w	4
G2_info	dc.w	FREEHORIZ!AUTOKNOB
	dc.w	0
	dc.w	0
	dc.w	$ffff/15
	ds.w	7
Colour_Gadget3	dc.l	Colour_Gadget7
	dc.w	40+10,50+45,114,11
	dc.w	GADGHCOMP
	dc.w	RELVERIFY!GADGIMMEDIATE
	dc.w	PROPGADGET
	dc.l	G3_image,0
	dc.l	0
	dc.l	0,G3_info
	dc.w	3
	dc.l	0

G3_image	ds.w	4
G3_info	dc.w	FREEHORIZ!AUTOKNOB
	dc.w	0
	dc.w	0
	dc.w	$ffff/15
	ds.w	7

Colour_Gadget7	dc.l	0
	dc.w	11,66,20,30
	dc.w	GADGHBOX
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	C_Box,0
	dc.l	0
	dc.l	0,0
	dc.w	7
	dc.l	0

Colour_Gadget10	
	FM_GADGET	Colour_Gadget11,185,3,72,10,<"B&W">,1,4
Colour_Gadget11	
	FM_GADGET	Colour_Gadget12,185,3+(1*14),72,11,<"Antik">,1,4
Colour_Gadget12	
	FM_GADGET	Colour_Gadget13,185,3+(2*14),72,12,<"Copy">,1,4
Colour_Gadget13	
	FM_GADGET	Colour_Gadget14,185,3+(3*14),72,13,<"Exchange">,1,4
Colour_Gadget14	
	FM_GADGET	Colour_Gadget15,185,3+(4*14),72,14,<"Spread">,1,4
Colour_Gadget15	
	FM_GADGET	Colour_Gadget16,185,3+(5*14),72,5,<"Undo All">,1,4
Colour_Gadget16	
	FM_GADGET	Colour_Gadget17,185,3+(6*14),72,20,<"Default">,1,4
Colour_Gadget17	
	FM_GADGET	Colour_Gadget1,185,3+(7*14),72,4,<"EXIT">,1,4


NewWindow1	dc.w	110,45
	dc.w	270,116
	dc.b	-1,-1
	dc.l	GADGETUP!GADGETDOWN
	dc.l	ACTIVATE!SMART_REFRESH
	dc.l	Colour_Gadget0
	dc.l	0
	dc.l	0
	dc.l	0
	dc.l	0
	dc.w	140,100
	dc.w	140,100
	dc.w	CUSTOMSCREEN

Window1_Ptr	dc.l	0		; window pointer
Undo	dc.w	0		; undo
CG_Box	BOX	-1,-1,161,41,1,0
C_Box	BOX	-1,-1,20,30,1,0
 
COLOR	dc.l	0		; current color
MoverSize	dc.w	0		; size of mover ($ffff/15 or $ffff/7)

NewColor	move.w	#$ffff/15,d0		; edit a new color 
	cmp.l	#$1f,COLOR		; color is halfbrite ( >$1f) => mover is smaller !
	bls	NC0
	move.w	#$ffff/7,d0
NC0	move.w	d0,MoverSize
	

	move.l	Window1_Ptr,a1		; write color number
	move.l	wd_RPort(a1),a1
	move.l	a1,a2
	moveq.l	#1,d0
	CALLGRAF	SetAPen

	move.l	a2,a1	
	move.w	#12,d0
	move.w	#50+15+6+1+15+15+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen2,a0
	move.l	COLOR,d0
	add.l	d0,d0
	add.l	d0,a0
	moveq.l	#2,d0
	CALLGRAF	Text


	move.l	Window1_Ptr,a0		; get RGB and save for undo !
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	COLOR,d0
	CALLGRAF	GetRGB4
	move.w	d0,Undo
	move.w	d0,-(sp)		; remake prop-gadgets
	lsr.w	#8,d0
	and.w	#$f,d0
	moveq.l	#0,d1
	move.b	d0,d1
	mulu.w	MoverSize,d1
	lea.l	Colour_Gadget1,a0
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	moveq.l	#0,d2
	move.w	MoverSize,d3
	moveq.l	#0,d4
	move.w	#FREEHORIZ!AUTOKNOB,d0
	CALLINT	ModifyProp
	
	moveq.l	#0,d1
	move.w	(sp),d1
	lsr.w	#4,d1
	and.w	#$f,d1
	mulu.w	MoverSize,d1
	lea.l	Colour_Gadget2,a0
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	moveq.l	#0,d2
	move.w	MoverSize,d3
	moveq.l	#0,d4
	move.w	#FREEHORIZ!AUTOKNOB,d0
	CALLINT	ModifyProp

	moveq.l	#0,d1
	move.w	(sp)+,d1
	and.w	#$f,d1
	mulu.w	MoverSize,d1
	lea.l	Colour_Gadget3,a0
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	moveq.l	#0,d2
	move.w	MoverSize,d3
	moveq.l	#0,d4
	move.w	#FREEHORIZ!AUTOKNOB,d0
	CALLINT	ModifyProp

	move.l	Window1_Ptr,a1		; draw block for undo gadget
	move.l	wd_RPort(a1),a1
	move.l	COLOR,d0
	CALLGRAF	SetAPen
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	move.w	#10+1,d0	
	move.w	#15+50+1,d1
	move.w	#10+19+1,d2	
	move.w	#15+50+29+1,d3
	CALLGRAF	RectFill
	jmp	Print_HEX		; write mover positions as numbers

G0_Handler	cmp.b	#1,Colour_Mode		
	beq	Copy_Color
	cmp.b	#2,Colour_Mode
	beq	Exchange_Color
	cmp.b	#3,Colour_Mode
	beq	Spread_Color


	move.w	d6,d0		; color gadget was selected but 
	move.w	d7,d1		; which color ?
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel
	move.l	d0,COLOR
	jsr	NewColor		
	bra	Colour_wait

Zahlen	dc.b	"0123456789ABCDEF"
Zahlen2	dc.b	"000102030405060708090a0b0c0d0e0f"
	dc.b	"101112131415161718191a1b1c1d1e1f"
	
Print_HEX	move.l	Window1_Ptr,a0		; write mover position as hex number
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	COLOR,d0
	CALLGRAF	GetRGB4
	move.w	d0,d5

	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a2
	moveq.l	#1,d0
	CALLGRAF	SetAPen

	move.l	a2,a1	
	move.w	#114+50+4,d0
	move.w	#50+15+6+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen,a0
	move.l	d5,d0
	lsr.l	#8,d0
	and.l	#$f,d0	
	add.l	d0,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#114+50+4,d0
	move.w	#50+15+6+15+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen,a0
	move.l	d5,d0
	lsr.l	#4,d0
	and.l	#$f,d0
	add.l	d0,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#114+50+4,d0
	move.w	#50+15+6+15+15+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen,a0
	and.l	#$f,d5	
	add.l	d5,a0
	moveq.l	#1,d0
	CALLGRAF	Text
	rts
G1_HandlerA	jsr	ReadColor		; prop gadget was selected
	jsr	Print_HEX
	move.l	Window1_Ptr,a0		
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg
	tst.l	d0
	beq	G1_HandlerA
	move.l	d0,a1	
	CALLEXEC 	ReplyMsg
G1_HandlerB	jsr	ReadColor		;  " (but left button was released afterwards)
	jsr	Print_HEX
	bra	Colour_wait

ReadColor	move.l	Window1_Ptr,a0		; get mover position and set RGB 
	CALLINT	ViewPortAddress
	move.l	d0,a0
	moveq.l	#0,d1
	moveq.l	#0,d2
	moveq.l	#0,d3
	
	move.w	G1_info+2,d1
	divu	G1_info+6,d1
	move.w	G2_info+2,d2
	divu	G2_info+6,d2
	move.w	G3_info+2,d3
	divu	G3_info+6,d3
	move.l	COLOR,d0
	CALLGRAF	SetRGB4
	rts
UNDO_colour	move.l	Window1_Ptr,a0		; undo color
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	COLOR,d0
	move.w	Undo,d1
	move.w	d1,d2
	move.w	d2,d3
	lsr.w	#8,d1
	and.l	#$f,d1
	lsr.w	#4,d2
	and.l	#$f,d2
	and.l	#$f,d3
	CALLGRAF	SetRGB4		
	jsr	NewColor
	bra	Colour_wait
GetDepth	move.l	ScreenPtr1,a0		; get depth (HAM=4!)
	lea.l	sc_BitMap(a0),a0
	moveq.l	#0,d0
	move.b	bm_Depth(a0),d0
	move.l	d0,Depth
	rts


R	dc.b	"R"
G	dc.b	"G"
B	dc.b	"B"


RGB	move.l	Window1_Ptr,a1		; print out R, G , B
	move.l	wd_RPort(a1),a1
	move.l	a1,a2
	moveq.l	#1,d0
	CALLGRAF	SetAPen

	move.l	a2,a1	
	move.w	#40,d0
	move.w	#50+15+6+2,d1
	CALLGRAF	Move	

	lea.l	R,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#40,d0
	move.w	#50+15+6+15+2,d1
	CALLGRAF	Move	

	lea.l	G,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#40,d0
	move.w	#50+15+6+15+15+2,d1
	CALLGRAF	Move	

	lea.l	B,a0
	moveq.l	#1,d0
	CALLGRAF	Text
	rts	

	even
ColorBuffer	ds.w	64
UNDOBuffer	ds.w	64
UNDODepth	dc.l	0

* How to make B&W
*
* C=r+g+b    ; add all three parts red, green, blue
* r=g=b=C/3  ; new parts

Black_White	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	Depth,d0		; copy colors to buffer
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a0
	lea.l	ColorBuffer(pc),a1
.X	move.w	(a0)+,(a1)+
	dbra	d1,.X
	
	move.l	Depth,d0		; convert colors to b&W
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	lea.l	ColorBuffer(pc),a1
.Y	moveq.l	#0,d0
	add.b	(a1),d0
	moveq.l	#0,d2
	move.b	1(a1),d2
	move.b	d2,d3
	and.b	#$f,d3
	lsr.b	#4,d2
	add.b	d2,d0
	add.b	d3,d0
	and.l	#$ff,d0
	divu	#3,d0
	move.b	d0,(a1)+
	move.b	d0,d2
	lsl.b	#4,d0
	or.b	d2,d0
	move.b	d0,(a1)+
	dbra	d1,.Y
	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	lea.l	ColorBuffer(pc),a1
	move.l	Depth,d1		
	moveq.l	#0,d0
	bset	d1,d0
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay
	bra	IN2

* How to make ANTIK
*
* C=r+g+b    ; add all three parts red, green, blue
* r=C/3      ; new parts
* g=C/4
* b=C/5

ANTIK	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	Depth,d0		; copy colors to buffer
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a0
	lea.l	ColorBuffer(pc),a1
.X	move.w	(a0)+,(a1)+
	dbra	d1,.X
	
	move.l	Depth,d0		; convert colors to b&W
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	lea.l	ColorBuffer(pc),a1
.Y	moveq.l	#0,d0
	add.b	(a1),d0
	moveq.l	#0,d2
	move.b	1(a1),d2
	move.b	d2,d3
	and.b	#$f,d3
	lsr.b	#4,d2
	add.b	d2,d0
	add.b	d3,d0
	and.l	#$ff,d0
	move.l	d0,d5
	divu	#3,d0
	move.b	d0,(a1)+
	move.l	d5,d0
	lsl.w	#2,d0
	and.w	#$f0,d0
	divu	#5,d5
	or.b	d5,d0
	move.b	d0,(a1)+
	dbra	d1,.Y
	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	lea.l	ColorBuffer(pc),a1
	move.l	Depth,d1		
	moveq.l	#0,d0
	bset	d1,d0
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay
	bra	IN2


SaveForUndo	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	Depth,d0		
	moveq.l	#0,d1
	bset	d0,d1
	cmp.b	#32,d1
	bls	.Z
	move.l	#32,d1
.Z	move.l	d1,UNDODepth
	subq	#1,d1

	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a0
	lea.l	UNDOBuffer(pc),a1
.X	move.w	(a0)+,(a1)+
	dbra	d1,.X		
	rts

UNDO_ALL	move.l	Window1_Ptr,a0		; restor save color map		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	lea.l	UNDOBuffer(pc),a1
	move.l	UNDODepth,d0		
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay
	bra	IN3


Colour_Mode	dc.b	0
OldColour_Mode	dc.b	$ff
	even


Title0	dc.b	"   Colours   ",0
Title1	dc.b	"  Copy to :  ",0
Title2	dc.b	"Exchange to :",0
Title3	dc.b	" Spread to : ",0 

COPY	cmp.b	#1,Colour_Mode
	beq	.Label1
	move.b	#1,Colour_Mode
	bra	Colour_wait
.Label1	clr.b	Colour_Mode
	bra	Colour_wait

Copy_Color	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a3	; pointer color map

	move.w	d6,d0		; get color		
	move.w	d7,d1		
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel
	
	move.l	d0,d3		; You can only change colours between 0 and $1f or between $20 and $3f
	move.l	COLOR,d4
	and.b	#$20,d3
	and.b	#$20,d4
	cmp.b	d3,d4
	bne	Colour_Error


	and.w	#$1f,d0		; change colours ( EHB colour-> normal colour) 
	add.w	d0,d0
	move.l	COLOR,d1
	and.w	#$1f,d1
	add.w	d1,d1



	move.w	(a3,d1),(a3,d0)	; copy it !
	clr.b	Colour_Mode

	CALLINT	RemakeDisplay		
	bra	IN2

EXCHANGE	cmp.b	#2,Colour_Mode
	beq	.Label1
	move.b	#2,Colour_Mode
	bra	Colour_wait
.Label1	clr.b	Colour_Mode
	bra	Colour_wait

Exchange_Color	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a3	; pointer color map

	move.w	d6,d0		; get color		
	move.w	d7,d1		
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel

	move.l	d0,d3		; You can only change colours between 0 and $1f or between $20 and $3f
	move.l	COLOR,d4
	and.b	#$20,d3
	and.b	#$20,d4
	cmp.b	d3,d4
	bne	Colour_Error

	and.l	#$1f,d0

	add.w	d0,d0
	move.l	COLOR,d1
	and.l	#$1f,d1
	add.w	d1,d1

	move.w	(a3,d1),d2		; exchange it !
	move.w	(a3,d0),(a3,d1)
	move.w	d2,(a3,d0)
	
	clr.b	Colour_Mode

	CALLINT	RemakeDisplay		
	bra	IN2

DefaultPalette	dc.w	$000,$eee,$775,$313,$04d,$997,$4b9,$553

DEFAULT	move.l	Window1_Ptr,a0		; set default colors
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	#16,d0
	lea.l	Palette,a1
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay
	jmp	IN2


SPREAD	cmp.b	#3,Colour_Mode
	beq	.Label1
	move.b	#3,Colour_Mode
	bra	Colour_wait
.Label1	clr.b	Colour_Mode
	bra	Colour_wait

Spread_Color	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a3	; pointer color map

	move.w	d6,d0		; get color		
	move.w	d7,d1		
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel
	move.l	COLOR,d1

P	cmp.w	d1,d0
	bls	.L1
	move.l	d0,d2
	move.l	d1,d0	
	move.l	d2,d1
.L1	move.l	d1,d2
	sub.l	d0,d2
	cmp.l	#1,d2
	bls	.exit

	move.l	d0,d3	; You can only change colours between 0 and $1f or between $20 and $3f
	move.l	d1,d4
	and.b	#$20,d3
	and.b	#$20,d4
	cmp.b	d3,d4
	bne	Colour_Error

	and.l	#$1f,d0
	and.l	#$1f,d1

	move.l	d0,FirstColor
	move.l	d1,LastColor
	move.l	d2,DiffColor
	move.l	d2,d6
	subq	#2,d6

	move.l	FirstColor,d0		;get RGB of first colour 
	bsr	ReadColorII
	movem.l	d0-d2,FirstR
	
	move.l	LastColor,d0		;get RGB of last colour 
	bsr	ReadColorII
	sub.l	FirstR,d0
	move.l	d0,DiffR		; What's the difference ?
	sub.l	FirstG,d1
	move.l	d1,DiffG
	sub.l	FirstB,d2
	move.l	d2,DiffB
	


	moveq.l	#6,d4		; convert integer to FFP
	lea.l	FirstR(pc),a3
.L2	move.l	(a3),d0
	CALLFFP	SPFlt
	move.l	d0,(a3)+
	dbra	d4,.L2

	lea.l	DiffR,a3
	lea.l	FaktorR,a4

	move.l	(a3)+,d0		; How much R (,G,B)  has to be added each colour ?
	move.l	DiffColor,d1
	CALLFFP	SPDiv
	move.l	d0,(a4)+
	move.l	(a3)+,d0
	move.l	DiffColor,d1
	CALLFFP	SPDiv
	move.l	d0,(a4)+
	move.l	(a3)+,d0
	move.l	DiffColor,d1
	CALLFFP	SPDiv
	move.l	d0,(a4)+

.L3	lea.l	FirstR,a3
	lea.l	FaktorR,a4
	lea.l	R_Wert,a5
	
	move.l	(a3),d0		; add to R,G,B
	move.l	(a4)+,d1
	CALLFFP	SPAdd
	move.l	d0,(a3)+
	CALLFFP	SPFix		; convert result to integer
	move.l	d0,(a5)+
	move.l	(a3),d0
	move.l	(a4)+,d1
	CALLFFP	SPAdd
	move.l	d0,(a3)+
	CALLFFP	SPFix
	move.l	d0,(a5)+
	move.l	(a3),d0
	move.l	(a4)+,d1
	CALLFFP	SPAdd
	move.l	d0,(a3)+
	CALLFFP	SPFix
	move.l	d0,(a5)+
	
	add.l	#1,FirstColor		; set colour
	move.l	FirstColor,d0
	movem.l	R_Wert,d1-d3
	bsr	WriteColor
	dbra	d6,.L3

	
	
.exit	clr.b	Colour_Mode
	CALLINT	RemakeDisplay		; remake copper list
	bra	IN2


FirstR	dc.l	0
FirstG	dc.l	0
FirstB	dc.l	0
DiffR	dc.l	0
DiffG	dc.l	0
DiffB	dc.l	0
DiffColor	dc.l	0	
FaktorR	dc.l	0
FaktorG	dc.l	0
FaktorB	dc.l	0
FirstColor	dc.l	0
LastColor	dc.l	0
ColorNumber	dc.l	0

R_Wert	dc.l	0
G_Wert	dc.l	0
B_Wert	dc.l	0
	


ReadColorII	move.l	d0,-(sp)
	move.l	Window1_Ptr,a0
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	(sp)+,d0
	move.l	vp_ColorMap(a0),a0
	and.l	#$1f,d0
	CALLGRAF	GetRGB4
	move.b	d0,d1
	move.b	d0,d2
	and.l	#$f,d2
	lsr.b	#4,d1
	and.l	#$f,d1	
	lsr.w	#8,d0
	and.l	#$f,d0
	rts

WriteColor	and.w	#$1f,d0
	move.l	d0,-(sp)
	move.l	Window1_Ptr,a0
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	(sp)+,d0
	CALLGRAF	SetRGB4
	rts
Colour_Error	move.l	ScreenPtr1,a0	; User tried to copy, spread or exchange a normal colour and a EHB colour!
	CALLINT	DisplayBeep
	clr.b	Colour_Mode
	bra	Colour_wait

********************* end of COLOUR


*
* goto sector
*


WindowPointer set Window2

Windowdef4	dc.w	60,50,310,100
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	GB_Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

GB_Gadget1	FM_GADGET	GB_Gadget5,5,80,56,1,<"Cancel">,1,2
GB_Gadget5	FM_GADGET	GB_Gadget6,61+5,80,56,5,<"First">,1,4
GB_Gadget6	FM_GADGET	GB_Gadget7,122+5,80,56,6,<"Last">,1,4
GB_Gadget7	FM_GADGET	GB_Gadget8,183+5,80,56,7,<"Offset">,1,4
GB_Gadget8	FM_GADGET	GB_Gadget3,244+5,80,56,8,<"Sector">,1,4



GB_Gadget3	dc.l	GB_Gadget4
	dc.w	146+5,40-12,10*8,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	GB_G1_Box,0,0,0,GB_G1_Info
	dc.w	100,0,0
GB_G1_Box	BOX	-1,-1,81,8,2,GB_G1_Box2
GB_G1_Box2	BOX	-2,-2,82,9,1,0
GB_G1_Info	dc.l	GB_G1_Puffer,GB_G1_Undo
	dc.w	0,10
	ds.w	12
GB_G1_Puffer	ds.b	12
GB_G1_Undo	ds.b	12	
GB_Gadget4	dc.l	0
	dc.w	146+5,65-12,10*8,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	GB_G1_Box,0,0,0,GB_G2_Info
	dc.w	101,0,0
GB_G2_Info	dc.l	GB_G2_Puffer,GB_G2_Undo
	dc.w	0,10
	ds.w	12
GB_G2_Puffer	ds.b	12
GB_G2_Undo	ds.b	12	


BLOCK	tst.b	FlagFile	; have you already opened a file ?		
	beq	.wait
	move.l	Offset,d0
	lea.l	GB_G2_Puffer(pc),a0
	jsr	MakeHex
	clr.b	(a0)	
	move.l	Offset,d0
	divu	#512,d0
	and.l	#$ffff,d0
	addq	#1,d0
	lea.l	GB_G1_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	

	lea.l	Windowdef4,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(300-13*8)/2,#11,#0,<"Goto sector :">
	WRITE	#5+(300-13*8)/2,#10,#1,<"Goto sector :">
	WRITE	#5+(300-19*8)/2,#40-6,#1,<"Sector : ">
	WRITE	#5+(300-19*8)/2,#65-6,#1,<"Offset : ">
	
	lea.l	GB_Gadget3(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	ActivateGadget
	

	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	moveq.l	#10,d1
	CALLDOS	Delay
	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
	cmp.b	#5,d0
	beq	FIRSTBLOCK
	cmp.b	#6,d0
	beq	LASTBLOCK
	cmp.b	#8,d0
	beq	GOTOBLOCK
	cmp.b	#100,d0
	beq	GOTOBLOCK
	cmp.b	#9,d0
	beq	GOTOOFFSET
	cmp.b	#101,d0
	beq	GOTOOFFSET
.wait	jmp	wait

***************************************

FIRSTBLOCK	tst.b	FlagFile
	beq	.Nothing
	jsr	WriteQuestion	; go to 1. sector
	moveq.l	#0,d0
	jmp	GetBlock
.Nothing	jmp	wait

***************************************
	
LASTBLOCK	tst.b	FlagFile
	beq	.Nothing
	jsr	WriteQuestion	; go to last sector
	move.l	Size,d0
	subq.l	#1,d0
	and.l	#-1-511,d0
	jmp	GetBlock
.Nothing	jmp	wait

****************************************

GOTOBLOCK	lea.l	GB_G1_Puffer(pc),a0	; go to special sector
	jsr	GetZahl
	tst.l	d1
	bne	.Wally
	tst.l	d0
	beq	WallyII
	subq.l	#1,d0
	mulu	#512,d0
	cmp.l	Size,d0
	bge	WallyII
	move.l	d0,.Sector
	jsr	WriteQuestion
	move.l	.Sector,d0	
	jmp	GetBlock
.Wally	move.l	ScreenPtr1,a0		; user is unable to enter a number correctly
	CALLINT	DisplayBeep
	jmp	BLOCK
.Sector	dc.l	0

WallyII	lea.l	Windowdef2,a0		; sector doesn't exist !
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-22*8)/2,#19+10,#0,<"Sector doesn't exist !">
	WRITE	#5+(290-22*8)/2,#18+10,#1,<"Sector doesn't exist !">
	move.l	Window2,a0		
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait	


****************************************

GOTOOFFSET	lea.l	GB_G2_Puffer(pc),a0	; go to special byte
	jsr	GetZahl
	tst.l	d1
	bne	.Wally
	cmp.l	Size,d0
	bge	WallyII
	move.l	d0,.OffsetC
	jsr	WriteQuestion
	move.l	.OffsetC,d0	
	and.l	#-1-511,d0
	
	move.l	d0,.Offset
	tst.l	Memory		; get block offset d0
	beq	.GetBlockDisk
	move.l	Memory,a0
	add.l	d0,a0
	lea.l	Puffer,a1
	lea.l	UndoPuffer,a2
	move.l	#511,d0
.L0	move.b	(a0),(a1)+	
	move.b	(a0)+,(a2)+	
	dbra	d0,.L0
	move.l	Size,d0
	sub.l	.Offset,d0
	cmp.l	#512,d0
	bls	.L3
	move.l	#512,d0
.L3	move.l	d0,AnzahlBytes
	move.l	.Offset,Offset
	jmp	.NewBlock

.GetBlockDisk	move.l	Filehandle,d1
	tst.l	d1
	beq	.Error
	move.l	.Offset,d2
	moveq.l	#OFFSET_BEGINNING,d3
	CALLDOS	Seek
	move.l	Filehandle,d1
	move.l	Size,d3
	sub.l	.Offset,d3
	cmp.l	#512,d3
	bls	.L1
	move.l	#512,d3
.L1	move.l	d3,d5
	move.l	#Puffer2,d2
	CALLDOS	Read
	cmp.l	d0,d5
	bne	OpenError
	lea.l	Puffer2,a0
	lea.l	Puffer,a1
	lea.l	UndoPuffer,a2
	move.l	#511,d0
.L2	move.b	(a0),(a1)+	
	move.b	(a0)+,(a2)+	
	dbra	d0,.L2
	move.l	d5,AnzahlBytes
	move.l	.Offset,Offset
.Error	
.NewBlock	jsr	SectorNumber
	tst.l	AnzahlBytes
	beq	NewMode
	jsr	ShowPuffer
	lea.l	Color,a0
	move.l	#511,d0
.L4	move.b	#3,(a0)+
	dbra	d0,.L4

	clr.l	Cursor
	move.l	.OffsetC,d0
	and.l	#511,d0
	jsr	SetCursor
	jsr	PrintPosition
	and.b	#1,Mode
	jmp	NewMode			

.Wally	move.l	ScreenPtr1,a0		; user is unable to enter a number correctly
	CALLINT	DisplayBeep
	jmp	BLOCK

.Offset	dc.l	0
.OffsetC	dc.l	0

****************************************



MakeZahl	movem.l	d0-d7/a1-a5,-(sp)	; convert d0 into ASCII (a0 ^buffer)
	tst.l	d0
	bpl.s	.MZ0
	move.b	#"-",(a0)+
	neg.l	d0
.MZ0	moveq.l	#7,d2
	moveq.l	#0,d3
	lea.l	.MZ_Potenzen(pc),a2
.MZ1	move.b	#"0"-1,d1
.MZ2	addq	#1,d1
	sub.l	(a2),d0
	bcc	.MZ2
	add.l	(a2)+,d0
	tst.b	d2
	beq	.MZ3
	cmp.b	#"0",d1
	beq	.MZ4
	moveq.l	#1,d3
	bra	.MZ3
.MZ4	tst.b	d3
	beq	.MZ5
.MZ3	move.b	d1,(a0)+
.MZ5	dbra	d2,.MZ1
	movem.l	(sp)+,d0-d7/a1-a5
	rts
.MZ_Potenzen	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1

**************************************

MakeHex	movem.l	d0-d7/a1-a5,-(sp)	; convert d0 into HEX (a0 ^buffer)
	tst.l	d0
	bpl	.MH0
	move.b	#"-",(a0)+
	neg.l	d0
.MH0	move.b	#"$",(a0)+
	move.l	d0,d1
	move.l	#32-4,d2
	lea.l	.Hex(pc),a1		
	moveq.l	#0,d3
.MH1	move.l	d1,d0
	lsr.l	d2,d0
	and.l	#$f,d0
	tst.l	d2
	beq	.MH3
	tst.b	d3
	bne	.MH3
	tst.b	d0
	beq	.MH2
.MH3	move.b	(a1,d0),(a0)+	
	moveq.l	#1,d3
.MH2	subq.l	#4,d2
	cmp.l	#-4,d2
	bne.s	.MH1
	movem.l	(sp)+,d0-d7/a1-a5
	rts		
.Hex	dc.b	"0123456789abcdef"


***************************************

GetZahl	movem.l	d2-d7/a0-a6,-(sp)	; convert ASCII to number
	bsr.s	.label20		; set a0 ^ASCII
	tst.l	.Invert
	beq.s	.L21
	neg.l	d0
.L21	movem.l	(sp)+,d2-d7/a0-a6	; result : d0= number, d1= error flag
	rts	
.Invert	dc.l	0

.label20	cmp.b	#" ",(a0)+
	beq.s	.label20
	tst.b	-(a0)
	clr.l	.Invert
	cmp.b	#"-",(a0)
	bne.s	.label21
	tst.b	(a0)+
	move.l	#-1,.Invert
.label21	move.l	a0,a1	
	cmp.b	#"$",(a0)		; Hex ?
	beq	.GetHex
	lea.l	.Potenzen-4(pc),a2	; Pointer to 10^x
	moveq.l	#0,d0		 
.label7	tst.b	(a0)+		 
	bne.s	.label7		; get last character 
	tst.b	-(a0)
.label8	move.b	-(a0),d1		 
	cmp.l	a1,a0		; already reached to first character
	bge.s	.label9		; no ! 

	moveq.l	#0,d1		; no error
	rts

.label9	tst.l	(a2)+		; next 10^x
	beq.s	.label12		; number too high !
	sub.b	#"0",d1		; 
	cmp.b	#9,d1		; useful ?
	bhi.s	.label12
	tst.b	d1		; add as many times as demanded
	beq.s	.label8		; 
	subq	#1,d1
	and.l	#15,d1
.label11	add.l	(a2),d0
	dbra	d1,.label11
	bra.s	.label8		; previous character

.label12	moveq.l	#-1,d1		; error !!!
	rts


.GetHex	tst.b	(a0)+		; do not read "$"
	move.l	a0,a1
	lea.l	.Hex(pc),a2		; Hex table
	moveq.l	#0,d0
	moveq.l	#-4,d2		; d2 = how often has the nibble to be moved to the left
				 
				 
.label13	tst.b	(a0)+		: get last character
	bne.s	.label13
	tst.b	-(a0)
.label14	move.b	-(a0),d1
	cmp.l	a1,a0		; already reached the first character
				
	bge.s	.label15
	moveq.l	#0,d1		; no error
	rts

.label15	addq.l	#4,d2		; nibbel has to be moved 4 bits more
	cmp.b	#32,d2		; number to high ?
	beq.s	.label12

	moveq.l	#-1,d3		; look up for ascii
.label16	addq	#1,d3
	cmp.b	#32,d3
	beq.s	.label12
	cmp.b	(a2,d3),d1
	bne.s	.label16
	and.l	#$f,d3		
	lsl.l	d2,d3		; move nibble to right position in the long word 
	add.l	d3,d0		; add it
	bra.s	.label14		; next character
			
.Potenzen	dc.l	1
	dc.l	10
	dc.l	100
	dc.l	1000
	dc.l	10000
	dc.l	100000
	dc.l	1000000
	dc.l	10000000
	dc.l	100000000
	dc.l	1000000000
	dc.l	0
.Hex	dc.b	"0123456789abcdef"
	dc.b	"0123456789ABCDEF"



***************************

* change size - add or sub bytes
*
*

Windowdef5	dc.w	60,40,310,130
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	SI_Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

SI_Gadget1	FM_GADGET	SI_Gadget2,5,110,70,1,<"Cancel">,1,2
SI_Gadget2	FM_GADGET	SI_Gadget3,80,110,130,2,<"Create new file">,1,4
SI_Gadget3	FM_GADGET	SI_Gadget4,215,110,90,3,<"Same file">,1,4



SI_Gadget4	dc.l	SI_Gadget5
	dc.w	146+5+16,60-12,10*8,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	SI_G4_Box,0,0,0,SI_G4_Info
	dc.w	4,0,0
SI_G4_Box	BOX	-1,-1,81,8,2,SI_G4_Box2
SI_G4_Box2	BOX	-2,-2,82,9,1,0
SI_G4_Info	dc.l	SI_G4_Puffer,SI_G4_Undo
	dc.w	0,10
	ds.w	12
SI_G4_Puffer	ds.b	12
SI_G4_Undo	ds.b	12	
SI_Gadget5	dc.l	SI_Gadget6
	dc.w	146+5+16,80-12,10*8,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	SI_G4_Box,0,0,0,SI_G5_Info
	dc.w	5,0,0
SI_G5_Info	dc.l	SI_G5_Puffer,SI_G5_Undo
	dc.w	0,10
	ds.w	12
SI_G5_Puffer	ds.b	12
SI_G5_Undo	ds.b	12	
SI_Gadget6	dc.l	0
	dc.w	146+5+16,100-12,10*8,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	SI_G4_Box,0,0,0,SI_G6_Info
	dc.w	5,0,0
SI_G6_Info	dc.l	SI_G6_Puffer,SI_G6_Undo
	dc.w	0,10
	ds.w	12
SI_G6_Puffer	ds.b	12
SI_G6_Undo	ds.b	12	

Size_Top	dc.l	0
Size_Bottom	dc.l	0
Size_Byte	dc.l	0

NS_Text	dc.b	"Size:          / New size:           "
	even

SIZE	tst.b	FlagFile	; have you already opened a file ?		
	beq	.wait
	tst.l	Memory	
	beq	RAM_ONLY
	jsr	WriteQuestion
	move.l	Size_Top,d0
	lea.l	SI_G4_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	move.l	Size_Bottom,d0
	lea.l	SI_G5_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	move.l	Size_Byte,d0
	lea.l	SI_G6_Puffer(pc),a0
	jsr	MakeHex
	clr.b	(a0)	


	lea.l	Windowdef5,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(300-18*8)/2,#11,#0,<"Change file size :">
	WRITE	#5+(300-18*8)/2,#10,#1,<"Change file size :">
	WRITE	#5+(300-23*8)/2,#60-6,#1,<"First byte :">
	WRITE	#5+(300-23*8)/2,#80-6,#1,<"Last byte  :">
	WRITE	#5+(300-23*8)/2,#100-6,#1,<"Byte       :">
	
	lea.l	SI_Gadget4(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	ActivateGadget

.NewSize	move.l	Size,d0
	lea.l	NS_Text+5(pc),a0
	jsr	SI_PrintSize			

	move.l	Size,d0
	add.l	Size_Top,d0
	add.l	Size_Bottom,d0
	lea.l	NS_Text+26(pc),a0
	jsr	SI_PrintSize		
	SETDRMD	#RP_JAM2
	SETBPEN	#7	


	WRITEMEM	#16,#30,#1,NS_Text,#35

	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	moveq.l	#10,d1
	CALLDOS	Delay
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0
	cmp.b	#3,d0
	bhi	.Number
	cmp.b	#1,d0
	bne	.ChangeSize
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait

.Number	lea.l	SI_G4_Puffer(pc),a0	; convert ASCII to hex
	jsr	GetZahl		; undo if error
	move.l	d1,d6
	tst.l	d1
	bne.s	.W1
	move.l	d0,Size_Top
.W1	lea.l	SI_G5_Puffer(pc),a0
	jsr	GetZahl
	add.l	d1,d6
	tst.l	d1
	bne.s	.W2
	move.l	d0,Size_Bottom
.W2	lea.l	SI_G6_Puffer(pc),a0
	jsr	GetZahl
	add.l	d1,d6
	cmp.l	#255,d0
	bhi.s	.W3
	tst.l	d6
	bne.s	.W3		
	move.l	d0,Size_Byte
	jmp	.NewSize

.W3	move.l	ScreenPtr1,a0		; error -> undo 
	CALLINT	DisplayBeep
	move.l	Size_Top,d0
	lea.l	SI_G4_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	move.l	Size_Bottom,d0
	lea.l	SI_G5_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	move.l	Size_Byte,d0
	lea.l	SI_G6_Puffer(pc),a0
	jsr	MakeHex
	clr.b	(a0)	
	lea.l	SI_Gadget4(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	jmp	.NewSize

.ChangeSize	subq.l	#2,d0
	move.l	d0,.Mode
	lea.l	SI_G4_Puffer(pc),a0	; convert ASCII to hex
	jsr	GetZahl		; go back if error
	move.l	d1,d6
	tst.l	d1
	bne.s	.Z1
	move.l	d0,Size_Top
.Z1	lea.l	SI_G5_Puffer(pc),a0
	jsr	GetZahl
	add.l	d1,d6
	tst.l	d1
	bne.s	.Z2
	move.l	d0,Size_Bottom
.Z2	lea.l	SI_G6_Puffer(pc),a0
	jsr	GetZahl
	add.l	d1,d6
	cmp.l	#255,d0
	bhi	.W3
	tst.l	d6
	bne	.W3		
	move.l	d0,Size_Byte
	move.l	Size,d0		; Does the user really
	add.l	Size_Top,d0		; want to create a file
	add.l	Size_Bottom,d0		; ( size >0 ) ????
	subq.l	#1,d0
	tst.l	d0
	bmi	.W3		; NO !!!
	move.l	Size_Top,d0
	neg.l	d0
	cmp.l	Size,d0
	bgt	.W3
	beq	.W3
	move.l	Size_Bottom,d0
	neg.l	d0
	cmp.l	Size,d0
	bgt	.W3
	beq	.W3
.Z3	move.l	Window2,a0
	CALLINT	CloseWindow
	move.l	.Mode,d0
	tst.l	Memory
	bne	SIZE_RAM
	jmp	wait
	
.Mode	dc.l	0




SI_PrintSize	movem.l	d0-d7/a0-a5,-(sp)
	move.b	#" ",d1
	tst.l	d0
	bpl.s	SI_PS0
	move.b	#"-",d1
	neg.l	d0
SI_PS0	move.b	d1,(a0)+
	moveq.l	#7,d2
	moveq.l	#0,d3
	lea.l	SI_Potenzen,a2
SI_PS1	move.b	#"0"-1,d1
SI_PS2	addq	#1,d1
	sub.l	(a2),d0
	bcc	SI_PS2
	add.l	(a2)+,d0
	tst.b	d2
	beq	SI_PS3
	cmp.b	#"0",d1
	beq	SI_PS4
	moveq.l	#1,d3
	bra	SI_PS3
SI_PS4	tst.b	d3
	bne	SI_PS3
	move.b	#" ",d1
SI_PS3	move.b	d1,(a0)+
	dbra	d2,SI_PS1
	movem.l	(sp)+,d0-d7/a0-a5
	rts
SI_Potenzen	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1


SIZE_RAM	tst.l	d0	; change size / edit in RAM
	beq	.NewFile	
	move.l	Size,d0
	add.l	Size_Top,d0
	add.l	Size_Bottom,d0
	move.l	d0,.NewSize
	moveq.l	#MEMF_PUBLIC,d1
	CALLEXEC	AllocMem	; get menory
	tst.l	d0
	bne.s	.CS1
	jmp	NoMemory
.CS1	move.l	d0,.NewMem	; fill with demanded byte
	move.l	d0,a0
	move.l	d0,a1
	move.l	.NewSize,d1
	subq.l	#1,d1
.CS2	move.b	Size_Byte+3,(a0)+
	dbra	d1,.CS2

	move.l	Memory,a0	; copy it
	move.l	Size,d1
	move.l	Size_Top,d0
	bpl	.CS3
	add.l	d0,d1
	neg.l	d0
	add.l	d0,a0
	bra	.CS4

.CS3	add.l	d0,a1
.CS4	move.l	Size_Bottom,d0
	tst.l	d0
	bpl	.CS5
	add.l	d0,d1
.CS5	subq.l	#1,d1	; -> DBRA

.CS6	move.b	(a0)+,(a1)+
	dbra	d1,.CS6

	move.l	Size,d0	; free memory used for the old file
	move.l	Memory,a1
	CALLEXEC	FreeMem
	move.l	.NewMem,Memory	; prepare for new file
	move.l	.NewSize,Size
	clr.b	FlagWrite
	jmp	NewFile

.NewMem	dc.l	0
.NewSize	dc.l	0
.File	dc.l	0
.Filehandle	dc.l	0

.NewFile	lea.l	.NF_Text(pc),a5	; save new file
	move.l	#R_G1,R_def+nw_FirstGadget
	jsr	FileRequester
	tst.l	d0
	beq	wait
	move.l	d0,.File

	move.l	#MODE_NEWFILE,d2
	move.l	d0,d1
	CALLDOS	Open
	tst.l	d0
	beq	OpenError
	move.l	d0,.Filehandle

	move.l	Memory,d6
	move.l	Size,d7
	move.l	Size_Top,d5
	subq.l	#1,d5
	tst.l	d5
	bmi	.K1
	
.K0	move.l	.Filehandle,d1	; write bytes at top
	moveq.l	#1,d3
	move.l	#Size_Byte+3,d2
	CALLDOS	Write
	cmp.l	#1,d0
	bne	DiskError
	dbra	d5,.K0
	bra.s	.K2
.K1	sub.l	Size_Top,d6	; add Size_Top  -(-x) = +x   
	add.l	Size_Top,d7
.K2	move.l	d6,d2	; write main part of file
	move.l	d7,d3
	tst.l	Size_Bottom
	bpl.s	.K3
	add.l	Size_Bottom,d3
.K3	move.l	d3,d5
	move.l	.Filehandle,d1
	CALLDOS	Write
	cmp.l	d5,d0
	bne	DiskError


	move.l	Size_Bottom,d5
	subq.l	#1,d5
	tst.l	d5
	bmi	.K4
	
.K5	move.l	.Filehandle,d1	; write bytes at bottom
	moveq.l	#1,d3
	move.l	#Size_Byte+3,d2
	CALLDOS	Write
	cmp.l	#1,d0
	bne	DiskError
	dbra	d5,.K5

.K4	move.l	.Filehandle,d1
	CALLDOS	Close
.wait	jmp	wait
	

.NF_Text	dc.b	"       Save File With New Size :        "
	even

****************************

SaveQuestion	move.l	(sp)+,.Return
	tst.b	FlagFile	; ask if you want to save file
	beq	.wait
	tst.l	Memory	; have you change anything or	
	beq	.wait	; do you edit on device ?
	tst.b	FlagWrite
	beq	.wait

	lea.l	Windowdef3,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-23*8)/2,#19,#0,<"Do you want to save the">
	WRITE	#6+(290-6*8)/2,#19+15,#0,<"file ?">
	WRITE	#5+(290-23*8)/2,#18,#1,<"Do you want to save the">
	WRITE	#5+(290-6*8)/2,#18+15,#1,<"file ?">


	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
	tst.b	d0
	bne	.wait

	lea.l	SAVE_Text(pc),a5	; Save file
	move.l	#R_G1,R_def+nw_FirstGadget
	jsr	FileRequester
	tst.l	d0
	beq	.wait
	move.l	d0,.File
	move.l	.File,d1		; Copy file from RAM to device
	move.l	#MODE_NEWFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	OpenError
	move.l	d0,Filehandle
	move.l	d0,d1
	move.l	Size,d3
	move.l	Memory,d2
	CALLDOS	Write
	cmp.l	Size,d0
	bne	DiskError
	
	move.l	Filehandle,d1
	CALLDOS	Close
	clr.b	FlagWrite
.wait	move.l	.Return,a0
	jmp	(a0)
.Return	dc.l	0
.File	dc.l	0


***************************


ChangeWrite	move.b	d0,WriteMode		; change write mode flag
	tst.b	d0
	beq.s	.Q1
	bclr.b	#0,M1_I3_Sub1+mi_Flags
.Q1	cmp.b	#1,d0
	beq.s	.Q2
	bclr.b	#0,M1_I3_Sub2+mi_Flags
.Q2	cmp.b	#2,d0
	beq.s	.Q3
	bclr.b	#0,M1_I3_Sub3+mi_Flags
.Q3	jmp	wait			
	
************************************

PrefName	dc.b	"FileMaster.Pref",0
	even
GetPrefs	move.l	#PrefName,d1		; load preferences
	move.l	#MODE_OLDFILE,d2	; if available  in 
	CALLDOS	Open		; the current dir
	tst.l	d0
	beq	.L1
	move.l	d0,d1
	move.l	d0,d6
	move.l	#(8*2)+2,d3
	move.l	#PREFS,d2
	CALLDOS	Read
	move.l	d6,d1
	CALLDOS	Close
.L1	rts

************************************

SavePrefs	move.l	Window1,a0		; save preferences
	CALLINT	ViewPortAddress	; copy colours
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a0
	lea.l	PREFS,a1
	moveq.l	#7,d0
.L0	move.w	(a0)+,(a1)+
	dbra	d0,.L0
	move.l	#PrefName,d1		
	move.l	#MODE_NEWFILE,d2	
	CALLDOS	Open		
	tst.l	d0
	beq	OpenError
	move.l	d0,d1
	move.l	d0,d6
	move.l	#(8*2)+2,d3
	move.l	#PREFS,d2
	CALLDOS	Write
	cmp.l	#(8*2)+2,d0
	bne	.DiskError
	move.l	d6,d1
	CALLDOS	Close
.L1	jmp	wait

.DiskError	move.l	d6,d1
	CALLDOS	Close
	jmp	OpenError	

************************************		

DefaultPrefs	lea.l	PREFS,a0	; use default prefs
	lea.l	DPrefs,a1
	move.l	#17,d0
.L1	move.b	(a1)+,(a0)+
	dbra	d0,.L1

	move.l	Window1,a0		; set colours
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	#16,d0
	lea.l	Palette,a1
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay

	move.b	WriteMode,d0		; set marks
	bset.b	#0,M1_I3_Sub1+mi_Flags
	bset.b	#0,M1_I3_Sub2+mi_Flags
	bset.b	#0,M1_I3_Sub3+mi_Flags
	tst.b	d0
	beq.s	.Q1
	bclr.b	#0,M1_I3_Sub1+mi_Flags
.Q1	cmp.b	#1,d0
	beq.s	.Q2
	bclr.b	#0,M1_I3_Sub2+mi_Flags
.Q2	cmp.b	#2,d0
	beq.s	.Q3
	bclr.b	#0,M1_I3_Sub3+mi_Flags
.Q3	jmp	wait
	
*************************************	

*
* who's the author of File Master ?????
*


WindowPointer set Window2

Windowdef6	dc.w	50,34,350,150
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	.Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

.Gadget1	FM_GADGET	0,150,130,50,0,<"OK">,1,4

ABOUT	jsr	INFO
	jmp	wait

INFO	lea.l	Windowdef6,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2

	move.l	d0,a0
	move.l	wd_RPort(a0),a0
	lea.l	FM_LOGO,a1
	moveq.l	#4,d1
	move.l	#(350-241)/2,d0
	CALLINT	DrawImage


	SETDRMD	#RP_JAM1

	WRITE	#6+(340-41*8)/2,#36+5,#0,<"Source  210 / Release 1.11/ Date 27.10.89">
	
	WRITE	#6+(340-24*8)/2,#51+5,#0,<"© by    ROGER FISCHLIN  ">
	WRITE	#6+(340-24*8)/2,#61+5,#0,<"        STEIGERWALDWEG 6">
	WRITE	#6+(340-24*8)/2,#71+5,#0,<"        D-6450 HANAU 7  ">
	WRITE	#6+(340-24*8)/2,#81+5,#0,<"        WEST GERMANY    ">
	WRITE	#6+(340-24*8)/2,#91+5,#0,<"Phone : (06181) 650266">
	
	WRITE	#6+(340-42*8)/2,#106+5,#0,<"This program is SHAREWARE - if you use it,">
	WRITE	#6+(340-42*8)/2,#116+5,#0,<"please send me US $10 (or DM 10 only EEC).">
	

	WRITE	#5+(340-41*8)/2,#35+5,#5,<"Source  210 / Release 1.11/ Date 27.10.89">
	
	WRITE	#5+(340-24*8)/2,#50+5,#1,<"© by    ROGER FISCHLIN  ">
	WRITE	#5+(340-24*8)/2,#60+5,#1,<"        STEIGERWALDWEG 6">
	WRITE	#5+(340-24*8)/2,#70+5,#1,<"        D-6450 HANAU 7  ">
	WRITE	#5+(340-24*8)/2,#80+5,#1,<"        WEST GERMANY    ">
	WRITE	#5+(340-24*8)/2,#90+5,#1,<"Phone : (06181) 650266">
	
	WRITE	#5+(340-42*8)/2,#105+5,#5,<"This program is SHAREWARE - if you use it,">
	WRITE	#5+(340-42*8)/2,#115+5,#5,<"please send me US $10 (or DM 10 only EEC).">
	
	move.l	Window2,a0
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	rts


****************************************

*
* search someting (HEX or ASCII)
*

WindowPointer set Window2

Windowdef7	dc.w	40,36,370,140
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	SE_Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

SE_Gadget1	FM_GADGET	SE_Gadget2,10,120,110,1,<"Cancel">,1,2
SE_Gadget2	FM_GADGET	SE_Gadget3,130,120,110,2,<"Search ASCII">,1,4
SE_Gadget3	FM_GADGET	SE_Gadget4,250,120,110,3,<"Search HEX">,1,4
SE_Gadget4	FM_GADGET	SE_Gadget5,210,50,70,4,<"First">,1,4
SE_Gadget5	FM_GADGET	SE_Gadget6,290,50,70,5,<"Last">,1,4
SE_Gadget6	FM_GADGET	SE_Gadget7,210,70,70,6,<"Yes">,1,4
SE_Gadget7	FM_GADGET	SE_Gadget8,290,70,70,7,<"No">,1,4
SE_Gadget8	FM_GADGET	SE_Gadget9,210,90,70,8,<"Forward">,1,4
SE_Gadget9	FM_GADGET	SE_Gadget10,290,90,70,9,<"Backward">,1,4


SE_Gadget10	dc.l	SE_Gadget11
	dc.w	11,30+1,43*8,10,GADGHCOMP,STRINGCENTER,STRGADGET
	dc.l	SE_G10_Box,0,0,0,SE_G10_Info
	dc.w	10,0,0
SE_G10_Box	BOX	-1,-1,344,8,2,SE_G10_Box2
SE_G10_Box2	BOX	-2,-2,345,9,1,0
SE_G10_Info	dc.l	SE_G10_Puffer,SE_G10_Undo
	dc.w	0,44
	ds.w	12
SE_G10_Puffer	ds.b	50
SE_G10_Undo	ds.b	50	

SE_Gadget11	dc.l	0
	dc.w	130,50+2,64,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	SE_G11_Box,0,0,0,SE_G11_Info
	dc.w	11,0,0
SE_G11_Box	BOX	-1,-1,64,8,2,SE_G11_Box2
SE_G11_Box2	BOX	-2,-2,65,9,1,0
SE_G11_Info	dc.l	SE_G11_Puffer,SE_G11_Undo
	dc.w	0,10
	ds.w	12
SE_G11_Puffer	ds.b	20
SE_G11_Undo	ds.b	20	

Xwait	jmp	wait

SEARCH	tst.b	FlagFile	; have you already opened a file ?		
	beq	Xwait
	move.l	Offset,d0
	divu	#512,d0
	and.l	#$ffff,d0
	addq	#1,d0
	move.l	d0,SearchSector
	lea.l	SE_G11_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	clr.l	SearchPos

	lea.l	Windowdef7,a0
	jsr	MakeWindow
	tst.l	d0
	beq	Xwait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(370-8*8)/2,#11,#0,<"Search :">
	WRITE	#5+(370-8*8)/2,#10,#1,<"Search :">
	WRITE	#10,#58,#1,<"Sector       :">
	WRITE	#10,#78,#1,<"Case sensitiv:">
	WRITE	#10,#98,#1,<"Direction    :">

S_NewPref	SETDRMD	#RP_JAM2		; write input
	SETBPEN	#7
	tst.l	SearchCase
	bne	.L1
	WRITE	#130,#78,#5,<"yes">
	bra	.L2
.L1	WRITE	#130,#78,#5,<"no ">
.L2	tst.l	SearchDirection
	bmi	.L3
	WRITE	#130,#98,#5,<"forward ">
	bra	.L4
.L3	WRITE	#130,#98,#5,<"backward">

.L4	lea.l	SE_Gadget11(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets
	

	
	lea.l	SE_Gadget10(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	ActivateGadget
	

.Message	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
	cmp.b	#3,d0
	bls	.Goon
	cmp.b	#6,d0
	beq	.YES
	cmp.b	#7,d0
	beq	.NO
	cmp.b	#8,d0
	beq	.FORWARD
	cmp.b	#9,d0
	beq	.BACKWARD
	cmp.b	#4,d0
	beq	.FIRST
	cmp.b	#5,d0
	beq	.LAST
	cmp.b	#11,d0
	beq	.Sector
	bra	.Message

.YES	clr.l	SearchCase
	bra	S_NewPref
.NO	move.l	#1,SearchCase
	bra	S_NewPref
.FORWARD	move.l	#1,SearchDirection
	bra	S_NewPref
.BACKWARD	move.l	#-1,SearchDirection
	bra	S_NewPref
.FIRST	move.w	#$3100,SE_G11_Puffer
	move.l	#1,SearchSector
	bra	S_NewPref
.LAST	move.l	Size,d0
	divu	#512,d0
	move.l	d0,d1
	swap	d1
	and.l	#$ffff,d0
	tst.w	d1
	beq.s	.SN1
	addq	#1,d0
.SN1	move.l	d0,SearchSector
	lea.l	SE_G11_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	bra	S_NewPref
.Sector	lea.l	SE_G11_Puffer(pc),a0
	jsr	GetZahl
	tst.l	d0
	beq	.Q2
	tst.l	d1
	beq	.Q1
.Q2	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
	move.l	SearchSector,d0
	lea.l	SE_G11_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	bra	S_NewPref
.Q1	move.l	d0,d1
	subq.l	#1,d1
	mulu	#512,d1
	cmp.l	Size,d1
	bge.s	.Q2
	move.l	d0,SearchSector	
	bra	.Message
	
.Goon	cmp.b	#2,d0
	beq	SearchString
	cmp.b	#3,d0
	beq	SearchHex
	moveq.l	#10,d1
	CALLDOS	Delay
	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
.wait	jmp	wait

SearchSector	dc.l	0
SearchDirection		
	dc.l	1
SearchCase	dc.l	0	
SearchMode	dc.l	-1

**************************************

SearchHex	and.l	#$ff,d0
	move.l	d0,SearchMode
	lea.l	SE_G11_Puffer(pc),a0	; check sector
	jsr	GetZahl
	tst.l	d0
	beq	.D2
	tst.l	d1
	beq	.Q1
.D2	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
	move.l	SearchSector,d0
	lea.l	SE_G11_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	bra	S_NewPref

.Q1	move.l	d0,d1
	subq.l	#1,d1
	mulu	#512,d1
	cmp.l	Size,d1
	bge.s	.Q2
	move.l	d0,SearchSector
	
	lea.l	SE_G10_Puffer(pc),a0	
	lea.l	CompPuffer,a1
	clr.b	(a1)

.Q2	move.b	(a0)+,d0		; skip all spaces and $
	tst.b	d0
	beq	.Q4
	cmp.b	#" ",d0
	beq.s	.Q2
	cmp.b	#"$",d0
	beq.s	.Q2
	move.b	d0,d2		; joker ?
	cmp.b	Joker,d0
	beq	.W5
	lea.l	.Hex(pc),a2		; ASCII -> Hex
	moveq.l	#-1,d2		; get 1. nibble
.W3	addq.l	#1,d2
	cmp.l	#32,d2
	bhi	.Error
	cmp.b	(a2)+,d0
	bne.s	.W3
	and.l	#$f,d2
	move.b	(a0)+,d0
	lea.l	.Hex(pc),a2		; ASCII -> Hex
	moveq.l	#-1,d3		; get 2. nibble
.W4	addq.l	#1,d3
	cmp.l	#32,d3
	bhi	.Error
	cmp.b	(a2)+,d0
	bne.s	.W4
	and.l	#$f,d3
	lsl.b	#4,d2
	or.b	d3,d2
.W5	move.b	d2,(a1)+		; copy to buffer
	bra	.Q2		

.Q4	move.l	a1,d0		; get length
	sub.l	#CompPuffer,d0
	move.l	d0,SearchLength
	tst.l	d0
	beq	.Q2
	move.l	Window2,a0
	CALLINT	CloseWindow
	jmp	J
	
.Error	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
	bra	S_NewPref


.Hex	dc.b	"0123456789abcdef"
	dc.b	"0123456789ABCDEF"

***************************************

SearchString	and.l	#$ff,d0
	move.l	d0,SearchMode
	lea.l	SE_G11_Puffer(pc),a0	; check sector
	jsr	GetZahl
	tst.l	d0
	beq	.Q2
	tst.l	d1
	beq	.Q1
.Q2	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
	move.l	SearchSector,d0
	lea.l	SE_G11_Puffer(pc),a0
	jsr	MakeZahl
	clr.b	(a0)	
	bra	S_NewPref

.Q1	move.l	d0,d1
	subq.l	#1,d1
	mulu	#512,d1
	cmp.l	Size,d1
	bge.s	.Q2
	move.l	d0,SearchSector
	
	lea.l	SE_G10_Puffer(pc),a0	; get length
	lea.l	CompPuffer,a1
	moveq.l	#-1,d0
	moveq.l	#0,d1
.Q3	addq.l	#1,d0
	moveq.l	#0,d1
	move.b	(a0)+,d1
	tst.b	d1
	beq	.Q3a
	tst.l	SearchCase
	beq	.Q3b
	cmp.b	Joker,d1
	beq	.Q3b
	cmp.b	#"A"-1,d1
	bls.s	.Q3b	
	cmp.b	#"Z",d1
	bhi.s	.Q3b	
	add.b	#"a"-"A",d1
.Q3b	move.b	d1,(a1)+
	bra	.Q3
		
.Q3a	move.l	d0,SearchLength
	tst.l	d0
	beq	.Q2
	move.l	Window2,a0
	CALLINT	CloseWindow	

J	jsr	Search_OpenWindow	
.E1	move.l	SearchSector,d0	
	tst.l	d0		; reached EOF ?
	beq	.NotFound
	move.l	d0,d1
	subq.l	#1,d1
	mulu	#512,d1
	cmp.l	Size,d1
	bge	.NotFound
	jsr	GetSearchSector	; get sector
	tst.l	d0		; error ?
	bne	.DiskError
	jsr	Search_Update		; write new sector number, check if user break
	tst.l	d0
	bne	.Cancel
	jsr	Search_it		; search it
	tst.l	d0		; found it ?
	bne	FOUND
	move.l	SearchSector,d0
	add.l	SearchDirection,d0
	move.l	d0,SearchSector
	bra	.E1
.NotFound	jsr	Search_CloseWindow
	jmp	NotFound
.DiskError	jsr	Search_CloseWindow
	jmp	OpenError
.Cancel	jsr	Search_CloseWindow
	jmp	wait


FOUND	jsr	Search_CloseWindow
	lea.l	SearchPufferII+64,a0	; show sector
	lea.l	Puffer,a1
	lea.l	UndoPuffer,a2
	move.l	#511,d0
.L0	move.b	(a0),(a1)+	
	move.b	(a0)+,(a2)+	
	dbra	d0,.L0
	move.l	SearchSector,d1
	subq.l	#1,d1
	mulu	#512,d1
	move.l	Size,d0
	sub.l	d1,d0
	cmp.l	#512,d0
	bls	.L3
	move.l	#512,d0
.L3	move.l	d0,AnzahlBytes
	move.l	d1,Offset
	jsr	SectorNumber
	jsr	ShowPuffer

	lea.l	Color,a0
	lea.l	SearchColors+64,a1
	move.l	#511,d0
.L1	move.b	(a1)+,(a0)+
	dbra	d0,.L1
	
WindowPointer set Window1
	
	SETAPEN	#4
	moveq.l	#0,d0		; show strings
	lea.l	Color,a0
.Z1	cmp.b	#4,(a0)+
	bne.s	.Z2
	movem.l	d0/a0,-(sp)	
	jsr	PutCursor
	movem.l	(sp)+,d0/a0
.Z2	addq.l	#1,d0
	cmp.l	#512,d0
	bne	.Z1

	lea.l	Color,a0
	moveq.l	#-1,d0
.Z4	addq.l	#1,d0
	cmp.l	#511,d0
	beq	.Z5
	cmp.b	#4,(a0)+
	bne.s	.Z4

	move.l	#1,Cursor
	jsr	SetCursor
	jmp	NewPos
.Z5	move.l	#1,Cursor
	moveq.l	#0,d0
	jsr	SetCursor
	jmp	NewPos





GetSearchSector	
	move.l	d0,-(sp)		; get sector
	move.b	CompPuffer,d1		; and some bytes of the
	eor.b	#$ff,d1		; previous sector and
	move.l	#1023,d2		; the next sector
	lea.l	SearchColors,a0	
	lea.l	SearchPuffer,a1	; fill buffer with
.G1	move.b	d1,(a1)+		; different byte
	move.b	#3,(a0)+
	dbra	d2,.G1
	
	move.l	(sp)+,d3	
	subq.l	#1,d3
	mulu	#512,d3
	sub.l	#64,d3		; get 64 bytes of previous sector
	move.l	#SearchPuffer,d4
	tst.l	d3		; first sector -> no bytes from previous sector 
	bpl	.G2
	add.l	#64,d4
	add.l	#64,d3
.G2	move.l	d3,d5
	add.l	#512+64+64,d5		; d3 = offset
	cmp.l	Size,d5		; d4 = ^buffer
	bls	.G3		; d5 = length
	move.l	Size,d5
.G3	sub.l	d3,d5
	tst.l	Memory
	beq	.Device
		
	move.l	d4,a1
	move.l	Memory,a0
	add.l	d3,a0
	subq.l	#1,d5
.G4	move.b	(a0)+,(a1)+
	dbra	d5,.G4


.G6a	lea.l	SearchPuffer,a0
	lea.l	SearchPufferII,a1
	move.l	#1023,d0
.O1	move.b	(a0)+,(a1)+
	dbra	d0,.O1

.G6	cmp.l	#2,SearchMode	
	bne	.G7
	tst.l	SearchCase
	beq	.G7


	lea.l	SearchPuffer,a0	; UpCase
	move.l	#512+64+64-1,d0
.G8	move.b	(a0)+,d1
	cmp.b	#"A"-1,d1
	bls.s	.G9	
	cmp.b	#"Z",d1
	bhi	.G9	
	add.b	#"a"-"A",d1
	move.b	d1,-1(a0)
.G9	dbra	d0,.G8	
.G7	moveq.l	#0,d0
	rts
.Device	move.l	d3,d2	; get sector from device
	move.l	Filehandle,d1
	moveq.l	#OFFSET_BEGINNING,d3
	CALLDOS	Seek
	move.l	Filehandle,d1
	move.l	d4,d2
	move.l	d5,d3
	CALLDOS	Read
	cmp.l	d5,d0	
	beq	.G6a
	moveq.l	#-1,d0
	rts

****************************************


* search it !!!

Search_it	move.l	#512+64+64-1,d0
	lea.l	SearchPuffer,a0
	lea.l	SearchColors,a1

.W1	move.l	a0,a3		; compare
	move.l	a1,a4
	lea.l	CompPuffer,a2
	move.l	SearchLength,d1
	subq.l	#1,d1
.W3	move.b	(a2)+,d3
	move.b	(a3)+,d4
	cmp.b	Joker,d3
	beq	.W5
	cmp.b	d3,d4
	bne	.W2
.W5	dbra	d1,.W3
	move.l	SearchLength,d1	; found it !!!
	subq.l	#1,d1
.W4	move.b	#4,(a4)+
	dbra	d1,.W4
.W2	tst.b	(a1)+
	tst.b	(a0)+
	dbra	d0,.W1
	
	moveq.l	#0,d2
	lea.l	SearchColors+64,a0
	move.l	#511,d0
.W6	cmp.b	#4,(a0)+
	bne.s	.W7
	moveq.l	#1,d2
.W7	dbra	d0,.W6

	move.l	d2,d0
	rts
	
	

SearchPos	dc.l	0
SearchLength	dc.l	0
SearchColors	ds.b	1024
SearchPuffer	ds.b	1024
SearchPufferII	ds.b	1024
CompPuffer	ds.b	100

************************************

*
* continue searching (forward) 
*

NEXT	tst.b	FlagFile		; file opened ?	
	beq	.nothing
	tst.l	SearchLength		; Have you entered a search string ?
	beq	.nothing				
	move.l	#1,SearchDirection	; forward
	move.l	Offset,d0		; start with current sector
	divu	#512,d0		; except File Master just
	and.l	#$ffff,d0		; found it on that sector
	addq.l	#1,d0
	cmp.l	SearchSector,d0
	bne.s	.W1
	add.l	SearchDirection,d0
.W1	move.l	d0,SearchSector
	jmp	J
.nothing	jmp	wait

************************************

*
* continue searching (backward) 
*

PREVIOUS	tst.b	FlagFile		; file opened ?	
	beq	.nothing
	tst.l	SearchLength		; Have you entered a search string ?
	beq	.nothing				
	move.l	#-1,SearchDirection	; backward
	move.l	Offset,d0		; start with current sector
	divu	#512,d0		; except File Master just
	and.l	#$ffff,d0		; found it on that sector
	addq.l	#1,d0
	cmp.l	SearchSector,d0
	bne.s	.W1
	add.l	SearchDirection,d0
.W1	move.l	d0,SearchSector
	jmp	J
.nothing	jmp	wait	



************************************

WindowPointer set Window2

NotFound	move.l	Offset,d0		; nex time : start with current sector
	divu	#512,d0		; 
	and.l	#$ffff,d0		; 
	addq.l	#1,d0
	move.l	d0,SearchSector

	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep	
	lea.l	Windowdef2,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-11*8)/2,#19+10,#0,<"Not found !">
	WRITE	#5+(290-11*8)/2,#18+10,#1,<"Not found !">
	move.l	Window2,a0		
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait

**************************************************

*
*	Search window
*


WindowPointer set Window2

Windowdef8	dc.w	60,50,300,70
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	.Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

.Gadget1	FM_GADGET	0,110,50,80,0,<"Cancel">,1,4

Search_OpenWindow	
	lea.l	Windowdef8,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.NoWindow
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-25*8)/2,#19,#0,<"Searching - please wait !">
	WRITE	#5+(290-25*8)/2,#18,#1,<"Searching - please wait !">
	SETDRMD	#RP_JAM2
	SETBPEN	#7
.NoWindow	rts

Search_Update	move.l	Window2,d0	; write new sector number
	tst.l	d0
	beq	.NoWindow
	move.l	#$20202020,SSector_text+10
	move.b	#" ",SSector_text+9
	lea.l	SSector_text+9(pc),a0
	move.l	SearchSector,d0
	jsr	MakeZahl
	WRITEMEM	#5+(290-11*8)/2,#35,#1,SSector_text,#15

	move.l	Window2,a0		; user break ??		
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg
	tst.l	d0
	beq	.NoWindow		
	move.l	d0,a1	
	CALLEXEC 	ReplyMsg
	moveq.l	#1,d0
.NoWindow	rts

	even
SSector_text	dc.b	"sector : .....            "

Search_CloseWindow			; close window	
	tst.l	Window2
	beq	.NoWindow
	move.l	Window2,a0
	CALLINT	CloseWindow
.NoWindow	rts



**************************************************

ChangeDisplay	move.b	d0,DisplayMode	; change Display (replace ASCII with ".")
	jsr	ShowPuffer

	move.l	Cursor,-(sp)
	lea.l	Color,a5
	moveq.l	#0,d7
.K1	cmp.b	#3,(a5)+
	beq	.K2
	movem.l	d7/a5,-(sp)
	move.l	d7,d0
	jsr	SetCursor
	movem.l	(sp)+,d7/a5
.K2	addq.l	#1,d7
	cmp.l	#512,d7
	bne.s	.K1
	move.l	(sp)+,d0
	jsr	SetCursor	
	jmp	wait

**************************************************

NEWCLI	move.l	#Nil,d1
	move.l	#1005,d2
	CALLDOS	Open
	tst.l	d0
	beq	.wait
	move.l	d0,d2
	move.l	d0,d3
	move.l	#NC,d1
	CALLDOS	Execute
	tst.l	d0
	bpl	.wait
	CALLINT	WBenchToFront	
.wait	jmp	wait
	even
NC	dc.b	"newcli",0
	even
Nil	dc.b	"NIL:",0
	even

************************************************

NEWSHELL	move.l	#Nil,d1
	move.l	#1005,d2
	CALLDOS	Open
	tst.l	d0
	beq	.wait
	move.l	d0,d2
	move.l	d0,d3
	move.l	#ns,d1
	CALLDOS	Execute
	tst.l	d0
	bpl	.wait
	CALLINT	WBenchToFront	
.wait	jmp	wait
	even
ns	dc.b	"newshell",0
	even

*************************************************

* which Amiga ( PAL / NTSC ) are you using ???

PAL_NTSC	move.l	4,a6
	cmp.b	#50,VBlankFrequency(a6)
	bne.s	NTSC_AMIGA
	move.l	#FONT_PAL1,FM_Font
	rts
NTSC_AMIGA	move.l	#FONT_NTSC1,FM_Font
	move.w	#207,Windowdef1+nw_Height
	move.w	#207,Screendef+ns_Height
	lea.l	NTSC,a0
	lea.l	PAL,a1
	move.l	#PAL_end-PAL-1,d0
.Q1	move.b	(a0)+,(a1)+
	dbra	d0,.Q1
	rts

**********************************************

*
* how much memory is available ???
*

WindowPointer set Window2

Windowdef9	dc.w	60,50,300,100
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	.Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

.Gadget1	FM_GADGET	0,125,80,50,0,<"OK">,1,4

MEMORY	lea.l	Windowdef9,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-6*8)/2,#19,#0,<"Memory">
	WRITE	#5+(290-6*8)/2,#18,#1,<"Memory">

	moveq.l	#MEMF_PUBLIC,d1
	CALLEXEC	AvailMem
	lea.l	Mem_text1+48-32(pc),a0
	jsr	MakeMZahl
	moveq.l	#MEMF_CHIP,d1
	CALLEXEC	AvailMem
	lea.l	Mem_text2+48-32(pc),a0
	jsr	MakeMZahl
	moveq.l	#MEMF_FAST,d1
	CALLEXEC	AvailMem
	lea.l	Mem_text3+48-32(pc),a0
	jsr	MakeMZahl
	move.l	#MEMF_PUBLIC!MEMF_LARGEST,d1
	CALLEXEC	AvailMem
	lea.l	Mem_text1+58-32(pc),a0
	jsr	MakeMZahl
	move.l	#MEMF_CHIP!MEMF_LARGEST,d1
	CALLEXEC	AvailMem
	lea.l	Mem_text2+58-32(pc),a0
	jsr	MakeMZahl
	move.l	#MEMF_FAST!MEMF_LARGEST,d1
	CALLEXEC	AvailMem
	lea.l	Mem_text3+58-32(pc),a0
	jsr	MakeMZahl
		
	WRITEMEM	#5+(290-34*8)/2,#40,#1,Mem_text1,#34
	WRITEMEM	#5+(290-34*8)/2,#50,#1,Mem_text2,#34
	WRITEMEM	#5+(290-34*8)/2,#60,#1,Mem_text3,#34

	move.l	Window2,a0		
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp	wait

Mem_text1	dc.b	"Public memory : ........ /........"
	even
Mem_text2	dc.b	"CHIP   memory : ........ /........"
	even
Mem_text3	dc.b	"FAST   memory : ........ /........"
	even 


MakeMZahl	movem.l	d0-d7/a1-a5,-(sp)	; convert d0 into ASCII (a0 ^buffer)
	tst.l	d0
	moveq.l	#7,d2
	moveq.l	#0,d3
	lea.l	.MZ_Potenzen(pc),a2
.MZ1	move.b	#"0"-1,d1
.MZ2	addq	#1,d1
	sub.l	(a2),d0
	bcc	.MZ2
	add.l	(a2)+,d0
	tst.b	d2
	beq	.MZ3
	cmp.b	#"0",d1
	beq	.MZ4
	moveq.l	#1,d3
	bra	.MZ3
.MZ4	tst.b	d3
	bne	.MZ3
	move.b	#" ",d1
.MZ3	move.b	d1,(a0)+
	dbra	d2,.MZ1
	movem.l	(sp)+,d0-d7/a1-a5
	rts
.MZ_Potenzen	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1


**********************************************

*
* Enter new joker
*


WindowPointer set Window2

Windowdef10	dc.w	60,50,300,70
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	JO_Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

JO_Gadget1	FM_GADGET	JO_Gadget2,50,50,60,1,<"OK">,1,4
JO_Gadget2	FM_GADGET	JO_Gadget3,200,50,60,0,<"Cancel">,1,4

JO_Gadget3	dc.l	0
	dc.w	(300-24)/2,30,24,10,GADGHCOMP,RELVERIFY,STRGADGET
	dc.l	JO_G3_Box,0,0,0,JO_G3_Info
	dc.w	3,0,0
JO_G3_Box	BOX	-1,-1,24,8,2,JO_G3_Box2
JO_G3_Box2	BOX	-2,-2,25,9,1,0
JO_G3_Info	dc.l	JO_G3_Puffer,JO_G3_Undo
	dc.w	0,4
	ds.w	12
JO_G3_Puffer	ds.b	6
JO_G3_Undo	ds.b	6



NewJoker	move.b	Joker,JO_G3_Puffer
	clr.b	JO_G3_Puffer+1
	lea.l	Windowdef10,a0
	jsr	MakeWindow
	tst.l	d0
	beq	.K3
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(290-15*8)/2,#19,#0,<"Enter new joker">
	WRITE	#5+(290-15*8)/2,#18,#1,<"Enter new joker">
	
.wait	lea.l	JO_Gadget3(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	ActivateGadget


	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	cmp.w	#1,gg_GadgetID(a4)
	beq	.K1
	tst.b	JO_G3_Puffer		; check if correct (no " " , no "$")
	beq.s	.K2
	cmp.b	#" ",JO_G3_Puffer
	beq.s	.K2
	cmp.b	#"$",JO_G3_Puffer
	beq.s	.K2
	tst.b	JO_G3_Puffer+1		; correct size (1)
	bne.s	.K2
	
	jmp	.K1
.K2	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
	move.b	Joker,JO_G3_Puffer
	clr.b	JO_G3_Puffer+1
	bra	.wait

.K1	move.l	#10,d1
	CALLDOS	Delay
	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0
	cmp.b	#1,d0
	beq.s	.K3
	move.b	JO_G3_Puffer,Joker	; copy new joker	
	move.b	JO_G3_Puffer,JoString+12	
	move.l	#10,d1
	CALLDOS	Delay
.K3	jmp	wait


**********************************************

Exec_Puffer1	ds.b	44
Exec_Puffer2	dc.b	":",0
	ds.b	302
Exec_File	dc.l	0
Exec_Filehandle	dc.l	0
Exec_Memory	dc.l	0
Exec_Size	dc.l	0
Exec_Seek	dc.l	0

EXECUTE	tst.b	FlagFile
	beq	.wait
	move.l	#1,R_Read		; don't display old file list
	lea.l	Exec_Puffer1(pc),a0	; exchange file name and drawer
	lea.l	R_File,a1
	move.l	#43,d0
.L1	move.b	(a1),d1
	move.b	(a0),(a1)+
	move.b	d1,(a0)+
	dbra	d0,.L1
	lea.l	R_Drawer,a1
	move.l	#301,d0
.L2	move.b	(a1),d1
	move.b	(a0),(a1)+
	move.b	d1,(a0)+
	dbra	d0,.L2

	jsr	WriteQuestion
	
	lea.l	Exec_Text(pc),a5	; get file to execute
	move.l	#R_G1c,R_def+nw_FirstGadget
	jsr	FileRequester
	move.l	d0,Exec_File	

	lea.l	Exec_Puffer1(pc),a0	; restore file name and drawer
	lea.l	R_File,a1
	move.l	#43,d0
.L3	move.b	(a0),d1
	move.b	(a1),(a0)+
	move.b	d1,(a1)+
	dbra	d0,.L3
	lea.l	R_Drawer,a1
	move.l	#301,d0
.L4	move.b	(a0),d1
	move.b	(a1),(a0)+
	move.b	d1,(a1)+
	dbra	d0,.L4
	move.l	#1,R_Read
	tst.l	Exec_File
	bne	Show_Patch
.wait	move.l	#1,R_Read		; don't display old file list
	jmp	wait


Exec_Text	dc.b	"             Execute  File:             "
	even

Show_Patch	move.l	Exec_File,d1		; Copy file to RAM
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	.OpenError
	
	move.l	d0,Exec_Filehandle
	
	move.l	d0,d1	; get file size
	moveq.l	#0,d2
	moveq.l	#OFFSET_END,d3
	CALLDOS	Seek

	move.l	Exec_Filehandle,d1	
	moveq.l	#0,d2
	moveq.l	#OFFSET_CURRENT,d3
	CALLDOS	Seek
	move.l	d0,Exec_Size

	move.l	Exec_Filehandle,d1	
	moveq.l	#0,d2
	moveq.l	#OFFSET_BEGINNING,d3
	CALLDOS	Seek

	tst.l	Exec_Size
	beq	.EmptyFile

	addq.l	#1,d0
	moveq.l	#MEMF_PUBLIC,d1
	CALLEXEC	AllocMem
	tst.l	d0
	beq	.NoMemory	
	move.l	d0,Exec_Memory
	
	move.l	d0,d2
	move.l	Exec_Filehandle,d1
	move.l	Exec_Size,d3
	CALLDOS	Read
	cmp.l	Exec_Size,d0
	bne	.DiskError

	add.l	Exec_Memory,d0		; put 0 Byte at end of patch
	move.l	d0,a0
	clr.b	(a0)
	
	move.l	Exec_Filehandle,d1
	CALLDOS	Close
	

	jsr	Display_Patch
	tst.l	d0
	bne	.T1
	jsr	Do_Patch
	cmp.l	#2,d0
	bne	.T1
	move.l	Exec_Memory,a1
	move.l	Exec_Size,d0
	addq.l	#1,d0
	CALLEXEC	FreeMem
	jmp	OpenError
.T1	move.l	Exec_Memory,a1
	move.l	Exec_Size,d0
	addq.l	#1,d0
	CALLEXEC	FreeMem
	move.l	Offset,d0
	jmp	GetBlock

.NoMemory	move.l	Exec_Filehandle,d1
	CALLDOS	Close	
	jmp	NoMemory
.EmptyFile	move.l	Exec_Filehandle,d1
	CALLDOS	Close	
	jmp	EmptyFile
.DiskError	move.l	Exec_Filehandle,d1
	CALLDOS	Close	
.OpenError	jmp	OpenError

; +++++++++++++++++++++++++++++++++++++++++++++++++++++

; Display patch !!!

WindowPointer set Window2

Windowdef11	dc.w	8,26,464,165
	dc.b	-1,-1
	dc.l	GADGETUP!RAWKEY
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	DI_Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

DI_Gadget1	FM_GADGET	DI_Gadget2,20,145,90,1,<"Cancel">,1,4
DI_Gadget2	FM_GADGET	DI_Gadget3,464-90-21,145,90,0,<"Execute">,1,4
DI_Gadget3	FM_GADGET	DI_Gadget4,131,145,90,2,<"Move down">,1,2
DI_Gadget4	FM_GADGET	0,242,145,90,3,<"Move Up">,1,2



DI_Text	ds.b	14*56

DT_Box	BOX	-1,-1,56*8,14*8,1,0
DI_Pos	dc.l	0

Display_Patch	lea.l	Windowdef11,a0			; Display top of patch
	jsr	MakeWindow
	tst.l	d0
	beq	.D1
	move.l	d0,Window2
	SETDRMD	#RP_JAM1
	WRITE	#6+(454-7*8)/2,#14,#0,<"Execute">
	WRITE	#5+(454-7*8)/2,#13,#1,<"Execute">
	clr.l	DI_Pos
	
	lea.l	DT_Box(pc),a1
	move.l	Window2,a0
	move.l	wd_RPort(a0),a0
	moveq.l	#8,d0
	move.l	#30-6,d1
	CALLINT	DrawBorder	

.loop	lea.l	DI_Text(pc),a0		; clear buffer
	move.l	#(14*56)-1,d0
.F0	move.b	#" ",(a0)+
	dbra	d0,.F0
	
	move.l	Exec_Memory,a0		; copy file to puffer
	add.l	DI_Pos,a0
	move.l	#13,d7
	lea.l	DI_Text,a1
.F1	moveq.l	#0,d6
	move.l	a1,a2
.F2	move.b	(a0)+,d0
	tst.b	d0
	beq	.F3
	cmp.b	#10,d0	
	beq	.F4
	addq.l	#1,d6
	cmp.b	#56,d6
	bhi	.F2
	move.b	d0,(a2)+
	bra	.F2
.F4	lea.l	56(a1),a1
	dbra	d7,.F1

.F3	SETDRMD	#RP_JAM2
	SETBPEN	#0
	WRITEMEM	#8,#30,#6,DI_Text,#56
	WRITEMEM	#8,#30+(1*8),#6,DI_Text+(1*56),#56
	WRITEMEM	#8,#30+(2*8),#6,DI_Text+(2*56),#56
	WRITEMEM	#8,#30+(3*8),#6,DI_Text+(3*56),#56
	WRITEMEM	#8,#30+(4*8),#6,DI_Text+(4*56),#56
	WRITEMEM	#8,#30+(5*8),#6,DI_Text+(5*56),#56
	WRITEMEM	#8,#30+(6*8),#6,DI_Text+(6*56),#56
	WRITEMEM	#8,#30+(7*8),#6,DI_Text+(7*56),#56
	WRITEMEM	#8,#30+(8*8),#6,DI_Text+(8*56),#56
	WRITEMEM	#8,#30+(9*8),#6,DI_Text+(9*56),#56
	WRITEMEM	#8,#30+(10*8),#6,DI_Text+(10*56),#56
	WRITEMEM	#8,#30+(11*8),#6,DI_Text+(11*56),#56
	WRITEMEM	#8,#30+(12*8),#6,DI_Text+(12*56),#56
	WRITEMEM	#8,#30+(13*8),#6,DI_Text+(13*56),#56
	


.wait	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	cmp.l	#RAWKEY,d4
	beq	.Key
	moveq.l	#0,d0
	move.w	gg_GadgetID(a4),d0
	cmp.b	#2,d0
	beq	.down
	cmp.b	#3,d0
	beq	.up
	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0
	move.w	gg_GadgetID(a4),d0
.D1	rts
.Key	cmp.b	#$4d,d5	
	beq.s	.down
	cmp.b	#$4c,d5
	bne	.wait
		
.up	move.l	Exec_Memory,a0		; move up
	move.l	DI_Pos,d7
	add.l	d7,a0
.U1	tst.l	d7		; reached top ?
	beq	.U2
	subq.l	#1,d7
	cmp.b	#10,-(a0)
	bne	.U1
.U3	tst.l	d7	
	beq	.U2
	subq.l	#1,d7
	cmp.b	#10,-(a0)
	bne	.U3
	addq.l	#1,d7		; Return belongs to previous line !
.U2	move.l	d7,DI_Pos
	bra	.loop

.down	move.l	Exec_Memory,a0		; move down
	move.l	DI_Pos,d7
	add.l	d7,a0
.P1	tst.b	(a0)		; reached EOF ?
	beq	.P2
	addq.l	#1,d7
	cmp.b	#10,(a0)+
	bne	.P1
.P2	move.l	d7,DI_Pos
	bra	.loop
	

; +++++++++++++++++++++++++++++++++++++++++++++++++++

*
* execute the patch !!!!
*

WindowPointer set Window2

Windowdef12	dc.w	8,80,464,70
	dc.b	-1,-1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!RMBTRAP
	dc.l	DP_Gadget1
	dc.l	0
	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

DP_Gadget1	FM_GADGET	DP_Gadget2,71,50,60,0,<"Cancel">,1,4
DP_Gadget2	FM_GADGET	DP_Gadget3,71+71+60,50,60,1,<"OK">,1,4
DP_Gadget3	FM_GADGET	0,71+71+71+60+60,50,60,2,<"Skip">,1,4


DP_Text	ds.b	56

DP_Box	BOX	-3,-3,56*8+2,10,1,DP_Box2
DP_Box2	BOX	-2,-2,56*8+1,9,0,DP_Box3
DP_Box3	BOX	-1,-1,56*8,8,0,0


Do_Patch	lea.l	Windowdef12,a0		
	jsr	MakeWindow
	tst.l	d0
	beq	.End
	move.l	d0,Window2

	lea.l	DP_Gadget2(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	OffGadget

	lea.l	DP_Gadget3(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	OffGadget
	
	SETDRMD	#RP_JAM1
	WRITE	#6+(454-9*8)/2,#14,#0,<"Executing">
	WRITE	#5+(454-9*8)/2,#13,#1,<"Executing">
	
	lea.l	DP_Box(pc),a1
	move.l	Window2,a0
	move.l	wd_RPort(a0),a0
	moveq.l	#8,d0
	move.l	#30-6,d1
	CALLINT	DrawBorder	


	move.l	Exec_Memory,a4	
	
.NewLine	move.l	Window2,a0		; user break ?
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg	
	tst.l	d0	
	beq.s	.F100
	move.l	d0,a1	
	CALLEXEC 	ReplyMsg
	jmp	.Exit


.F100	lea.l	DP_Text(pc),a0		; clear buffer
	move.l	#55,d0
.F0	move.b	#" ",(a0)+
	dbra	d0,.F0
	move.l	a4,-(sp)
	
	lea.l	DP_Text,a1		; copy to buffer
.F1	moveq.l	#0,d6
.F2	move.b	(a4)+,d0
	tst.b	d0
	beq	.F4
	cmp.b	#10,d0	
	beq	.F4
	addq.l	#1,d6
	cmp.b	#56,d6
	bhi	.F2
	move.b	d0,(a1)+
	bra	.F2
.F4	SETDRMD	#RP_JAM2		; show current line
	SETBPEN	#0
	WRITEMEM	#8,#30,#6,DP_Text,#56


	move.l	(sp)+,a4
	move.l	a4,.Line
.NewLine2	move.b	(a4)+,d0
	cmp.b	#9,d0		; skip tab and spaces 
	beq.s	.NewLine2
	cmp.b	#" ",d0
	beq.s	.NewLine2
	tst.b	d0
	beq	.End
	cmp.b	#";",d0		; comment ?
	beq	.Rem
	cmp.b	#"*",d0		; comment ?
	beq	.Rem
	cmp.b	#10,d0		; empty line ?
	beq	.NewLine
	bra	.Y1

.Rem	move.b	(a4)+,d0		; skip line
	tst.b	d0
	beq	.End
	cmp.b	#10,d0
	bne	.Rem
	bra	.NewLine

.Offset	ds.b	20
.Y1	lea.l	.Offset(pc),a0
	move.l	#18,d2
	move.b	d0,(a0)+
.Y2	move.b	(a4)+,d1	; copy into buufer until
	cmp.b	#" ",d1	; you encounter tab,space,":","="
	beq.s	.Y3
	cmp.b	#"=",d1
	beq.s	.Y3
	cmp.b	#":",d1
	beq.s	.Y3
	cmp.b	#9,d1
	beq.s	.Y3
	tst.b	d1
	beq	.Error
	move.b	d1,(a0)+
	dbra	d2,.Y2
	bra	.Error
.Y3	clr.b	(a0)	; convert ASCII into hex
	lea.l	.Offset(pc),a0
	jsr	GetZahl
	tst.l	d1
	bne	.Error
	move.l	d0,Exec_Seek	; compare offset to file size
	cmp.l	Size,d0
	bge	.Error
.Y4	move.b	(a4)+,d0	; skip spaces,tabs and $
	cmp.b	#" ",d0
	beq	.Y4
	cmp.b	#9,d0
	beq	.Y4
	cmp.b	#"$",d0
	beq	.Y4
	cmp.b	#":",d0
	beq	.Y4
	tst.b	d0
	beq	.Error
	cmp.b	#34,d0	; " ,' or ` -> ASCIIs
	beq	.ASCIIs
	cmp.b	#180,d0
	beq	.ASCIIs
	cmp.b	#$27,d0
	beq	.ASCIIs
	
	
	
.Y6	bsr	.GetHex	; get hex
	tst.l	d0
	bmi	.Error
	jsr	.Exec_Write
	cmp.l	#2,d0
	beq	.Error
	cmp.l	#1,d0
	bne	.DiskError	
	
.Y5	move.b	(a4)+,d0	; skip spaces,tabs and $
	cmp.b	#" ",d0
	beq	.Y5
	cmp.b	#9,d0
	beq	.Y5
	cmp.b	#"$",d0
	beq	.Y5
	tst.b	d0
	beq	.End
	cmp.b	#10,d0
	beq	.NewLine
	bra	.Y6

.Error	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep

	lea.l	DP_Gadget1(pc),a0	; switch off OK gadget
	move.l	Window2,a1		
	sub.l	a2,a2
	CALLINT	OnGadget
	lea.l	DP_Gadget2(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	OffGadget
	lea.l	DP_Gadget3(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	OnGadget
	
	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message		
	cmp.w	#2,gg_GadgetID(a4)
	bne	.L1
	move.l	.Line,a4		; skip the line
.L2	move.b	(a4)+,d0
	tst.b	d0		; EOF ?
	beq.s	.L1	
	cmp.b	#10,d0		; end of line ( $a) reached ?
	bne.s	.L2
	jmp	.NewLine

.L1	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#-1,d0
	rts

.End	SETDRMD	#RP_JAM2		
	SETBPEN	#0
	WRITE	#8,#30,#2,<"                    »»   Done !   ««                    ">

	
	lea.l	DP_Gadget1(pc),a0	; switch on only OK gadget
	move.l	Window2,a1		
	sub.l	a2,a2
	CALLINT	OffGadget
	lea.l	DP_Gadget2(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	OnGadget
	lea.l	DP_Gadget3(pc),a0
	move.l	Window2,a1
	sub.l	a2,a2
	CALLINT	OffGadget
	
	move.l	Window2,a0		; wait  until user selects cancel
	move.l	wd_UserPort(a0),a0
	CALLEXEC	WaitPort

.Exit	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#0,d0
	rts

.DiskError	move.l	Window2,a0
	CALLINT	CloseWindow
	moveq.l	#2,d0
	rts

	even
.Line	dc.l	0

;++++++++++

.Mark	dc.b	0	
	even
.ASCIIs	move.b	d0,.Mark
.T1	move.b	(a4)+,d0	; there must be at least a character
	tst.b	d0
	beq	.Error
	cmp.b	.Mark,d0
	beq	.Error
	cmp.b	#10,d0
	beq	.Error

.T2	bsr	.Exec_Write	; write it
	cmp.l	#2,d0
	beq	.Error
	cmp.l	#1,d0
	bne	.DiskError
	move.b	(a4)+,d0	
	beq	.Error
	cmp.b	.Mark,d0
	beq	.ASCII_End
	cmp.b	#10,d0
	beq	.NewLine
	bra	.T2

.ASCII_End	move.b	(a4)+,d0	; skip tabs and spaces
	cmp.b	#" ",d0
	beq.s	.ASCII_End
	cmp.b	#9,d0
	beq.s	.ASCII_End
	tst.b	d0
	beq	.End	; reached EOF ?
	cmp.b	#10,d0
	bne	.Error
	bra	.NewLine
	

.GetHex	lea.l	.Hex(pc),a2		; ASCII -> Hex
	moveq.l	#-1,d2		; get 1. nibble
.W3	addq.l	#1,d2
	cmp.l	#32,d2
	bhi	.GHError
	cmp.b	(a2)+,d0
	bne.s	.W3
	and.l	#$f,d2
	move.b	(a4)+,d0
	lea.l	.Hex(pc),a2		; ASCII -> Hex
	moveq.l	#-1,d3		; get 2. nibble
.W4	addq.l	#1,d3
	cmp.l	#32,d3
	bhi	.GHError
	cmp.b	(a2)+,d0
	bne.s	.W4
	and.l	#$f,d3
	lsl.b	#4,d2
	or.b	d3,d2
.W5	move.b	d2,d0
	rts
.GHError	moveq.l	#-1,d0
	rts
.Hex	dc.b	"0123456789abcdef"
	dc.b	"0123456789ABCDEF"


.Exec_Write	move.l	Memory,a0	; write d0 
	cmp.l	#0,a0
	beq	.Write_Device
	move.l	Exec_Seek,d1
	cmp.l	Size,d1
	bge	.J2
	add.l	Exec_Seek,a0
	move.b	d0,(a0)
	add.l	#1,Exec_Seek
	move.b	#1,FlagWrite	; something was changed !!!
	moveq.l	#1,d0
	rts
.J2	moveq.l	#2,d0
	rts
.Write_Device	move.l	d0,.Buffer	; write d0 on device
	move.l	Filehandle,d1
	move.l	Exec_Seek,d2
	moveq.l	#OFFSET_BEGINNING,d3
	CALLDOS	Seek	
	tst.l	d0
	bmi	.J1
	move.l	Filehandle,d1
	move.l	#.Buffer,d2
	moveq.l	#1,d3
	CALLDOS	Write
.J1	rts
.Buffer	dc.b	0

********************************************************

KEY_INFO	lea.l	Windowdef6,a0		; show keys
	jsr	MakeWindow
	tst.l	d0
	beq	.wait
	move.l	d0,Window2

	move.l	d0,a0
	move.l	wd_RPort(a0),a0
	lea.l	FM_LOGO,a1
	moveq.l	#4,d1
	move.l	#(350-241)/2,d0
	CALLINT	DrawImage


	SETDRMD	#RP_JAM1

	WRITE	#6+(340-8*8)/2,#46,#0,<"» Keys «"
	WRITE	#6+(340-38*8)/2,#61,#0,<"F1  - Open           F6  - Write      ">      
	WRITE	#6+(340-38*8)/2,#71,#0,<"F2  - Save As        F7  - Undo       "> 
	WRITE	#6+(340-38*8)/2,#81,#0,<"F3  - Execute        F8  - Search     ">
	WRITE	#6+(340-38*8)/2,#91,#0,<"F4  - Forward        F9  - Previous   ">      
	WRITE	#6+(340-38*8)/2,#101,#0,<"F5  - Backward       F10 - Next       ">
	WRITE	#6+(340-38*8)/2,#111,#0,<"ESC - Quit           Ins - Change Mode">
	
	WRITE	#5+(340-8*8)/2,#45,#6,<"» Keys «"
	WRITE	#5+(340-38*8)/2,#60,#5,<"F1  - Open           F6  - Write      ">      
	WRITE	#5+(340-38*8)/2,#70,#5,<"F2  - Save As        F7  - Undo       "> 
	WRITE	#5+(340-38*8)/2,#80,#5,<"F3  - Execute        F8  - Search     ">
	WRITE	#5+(340-38*8)/2,#90,#5,<"F4  - Forward        F9  - Previous   ">      
	WRITE	#5+(340-38*8)/2,#100,#5,<"F5  - Backward       F10 - Next       ">
	WRITE	#5+(340-38*8)/2,#110,#5,<"ESC - Quit           Ins - Change Mode">
	
	move.l	Window2,a0
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.wait	jmp 	wait


**********************************************************

* this section contains several images so it has to be in CHIP mem !!!



	Section 	"Pfeile",DATA_C

R_G9_PfeilUp	dc.w	0,0,15,9,1
	dc.l	R_G9_PU_image
	dc.b	1,0
	dc.l	0
R_G9_PU_image	dc.w	0
	dc.w	%0000000100000000
	dc.w	%0000001110000000
	dc.w	%0000011111000000
	dc.w	%0000111111100000
	dc.w	%0001111111110000
	dc.w	%0011111111111000
	dc.w	%0111111111111100
	dc.w	0
R_G10_PfeilD	dc.w	0,0,15,9,1
	dc.l	R_G10_PD_image
	dc.b	1,0
	dc.l	0
R_G10_PD_image	dc.w	0
	dc.w	%0111111111111100
	dc.w	%0011111111111000
	dc.w	%0001111111110000
	dc.w	%0000111111100000
	dc.w	%0000011111000000
	dc.w	%0000001110000000
	dc.w	%0000000100000000
	dc.w	0

FM_LOGO	dc.w	0,0,241,23,3
	dc.l	FM_Data
	dc.b	1+2+4,0
	dc.l	0
	even
FM_Data
	incbin	"FM.Image",0

	

FONT_PAL1 
	incbin	"Font1.data"
	even
FONT_NTSC1
	incbin	"Font3.Data"	

	even

FM_Font	dc.l	0
Puffer	ds.b	512
UndoPuffer	ds.b	512
Puffer2	ds.b	512
Color	ds.b	512
AnzahlBytes	dc.l	499
Cursor	dc.l	0
Memory	dc.l	0
Filehandle	dc.l	0
Size	dc.l	0
Offset	dc.l	0
Mode	dc.b	1
Nibble	dc.b	0
FlagFile	dc.b	0
FlagWrite	dc.b	0
DisplayMode	dc.b	0
	even
PREFS
Palette	dc.w	$000,$eee,$775,$313,$04d,$997,$4b9,$553
WriteMode	dc.b	0	;0=ask, 1=write, 2=do nothing
Joker	dc.b	"?"
	even
DPrefs	dc.w	$000,$eee,$775,$313,$04d,$997,$4b9,$553
	dc.b	0
	dc.b	"?"
	even


PAL
HEX_X	dc.l	24	; basic datas for PAL Amigas
HEX_Y	dc.l	11+14+3+6	; X and Y  position of Hex 
ASC_X	dc.l	(32+1+7)*8+22
ASC_Y	dc.l	11+14+3+6	; X and Y  position of ASCII
BaseLine	dc.l	6	; Baseline of font
Bytes_Per_Row	dc.l	16	; Bytes per row
Zeilen	dc.l	32	; number of lines
FONT_X	dc.l	8	; X size of font
FONT_Y	dc.l	7	; Y size of font
HEX_Breite	dc.l	35	; number of characters per  Hex   row (plus spaces)
ASC_Breite	dc.l	16	;   "     "     "       "   ASCII  "   
PAL_end
	

NTSC			; basic datas for NTSC Amigas
.HEX_X	dc.l	5
.HEX_Y	dc.l	11+14+3+7
.ASC_X	dc.l	328+5
.ASC_Y	dc.l	11+14+3+7
.BaseLine	dc.l	7
.Bytes_Per_Row	dc.l	24
.Zeilen	dc.l	22
.FONT_X	dc.l	6
.FONT_Y	dc.l	8
.HEX_Breite	dc.l	53
.ASC_Breite	dc.l	24

	Section	"NP",BSS

NamenPuffer	ds.b	38*300

	even

