%	this is a special MMIX BIOS for the StopWatch example
%	it is considert to be loaded into ROM 
%	at physical address 0000 0000 0000 0000
%	used with 
%	virtual address 8000 0000 0000 0000
	
	LOC	#8000000000000000

count	IS	$254
tmp	IS	$0

PbitMH	IS	#0001
	
Main	IS	@			keep mmixal happy
Boot	GETA	tmp,DTrap		set dynamic- and forced-trap  handler
	PUT	rTT,tmp
	PUT	rG,254		       make count a global register
	SETMH	tmp,PbitMH		the P bit
	NAND	tmp,tmp,tmp		invert
	PUT	rK,tmp               
1H	SYNC	4			go to power save mode
	JMP	1B            	and loop idle waiting for interrupts
	    
	    	
%	Entry point for a dynamic TRAP	
irq	IS	$1
inr	IS	$2
base	IS	$3

DTrap  GET 	irq,rQ
	SUBU	tmp,irq,1		from xxx...xxx1000 to xxx...xxx0111
	SADD	inr,tmp,irq		position of lowest bit
	ANDN	tmp,irq,tmp		the lowest bit
       ANDN	tmp,irq,tmp		delete lowest bit
	PUT	rQ,tmp			and store to rQ
	SLU	inr,inr,2		scale
       GETA	base,DTrapTable	and jump
	PUSHGO	tmp,base,inr
	SETMH	tmp,PbitMH		the P bit
	NAND	$255,tmp,tmp		invert and prepare for transfer to rK
	RESUME	1


DTrapTable JMP DTrapUnhandled  %0
	    JMP DTrapUnhandled  %1
           JMP DTrapUnhandled  %2
           JMP DTrapUnhandled  %3
           JMP DTrapUnhandled  %4
           JMP DTrapUnhandled  %5
           JMP DTrapUnhandled  %6
           JMP DTrapUnhandled  %7
           JMP DTrapStart      %8
           JMP DTrapStop       %9
           JMP DTrapReset      %10
           JMP DTrapTime       %11
           JMP DTrapUnhandled  %12
           JMP DTrapUnhandled  %13
           JMP DTrapUnhandled  %14
           JMP DTrapUnhandled  %15
           JMP DTrapUnhandled  %16
           JMP DTrapUnhandled  %17
           JMP DTrapUnhandled  %18
           JMP DTrapUnhandled  %19
           JMP DTrapUnhandled  %20
           JMP DTrapUnhandled  %21
           JMP DTrapUnhandled  %22
           JMP DTrapUnhandled  %23
           JMP DTrapUnhandled  %24
           JMP DTrapUnhandled  %25
           JMP DTrapUnhandled  %26
           JMP DTrapUnhandled  %27
           JMP DTrapUnhandled  %28
           JMP DTrapUnhandled  %29
           JMP DTrapUnhandled  %30
           JMP DTrapUnhandled  %31
           JMP DTrapUnhandled  %32
           JMP DTrapUnhandled  %33
           JMP DTrapUnhandled  %34
           JMP DTrapUnhandled  %35
           JMP DTrapUnhandled  %36
           JMP DTrapUnhandled  %37
           JMP DTrapUnhandled  %38
           JMP DTrapUnhandled  %39
           JMP DTrapUnhandled  %40
           JMP DTrapUnhandled  %41
           JMP DTrapUnhandled  %42
           JMP DTrapUnhandled  %43
           JMP DTrapUnhandled  %44
           JMP DTrapUnhandled  %45
           JMP DTrapUnhandled  %46
           JMP DTrapUnhandled  %47
           JMP DTrapUnhandled  %48
           JMP DTrapUnhandled  %49
           JMP DTrapUnhandled  %50
           JMP DTrapUnhandled  %51
           JMP DTrapUnhandled  %52
           JMP DTrapUnhandled  %53
           JMP DTrapUnhandled  %54
           JMP DTrapUnhandled  %55
           JMP DTrapUnhandled  %56
           JMP DTrapUnhandled  %57
           JMP DTrapUnhandled  %58
           JMP DTrapUnhandled  %59
           JMP DTrapUnhandled  %60
           JMP DTrapUnhandled  %61
           JMP DTrapUnhandled  %62
           JMP DTrapUnhandled  %63
           JMP DTrapUnhandled  %64  rQ was zero



DTrapUnhandled GETA	tmp,2F
		 SWYM	tmp,5               % inform the debugger
		 POP	0,0
2H		 BYTE	"DEBUG unhandled Interrupt",0

TimerHI	IS	#8002
tiOffset	IS	#10

DTrapStart	SETH	base,TimerHI		% timer address
		SET	tmp,100			% i = 100 ms
		ORMH	tmp,100        		% t = 100 ms
		STO	tmp,base,tiOffset	% enable timer interrupts
		POP 0,0

DTrapStop	SETH	base,TimerHI		% timer address
		STCO	0,base,tiOffset		% disable timer interrupts
		POP 0,0

DTrapReset	SET	count,0
		GET	tmp,rJ	
		PUSHJ	tmp+1,displaycount
		PUT	rJ,tmp
		POP 0,0
		
DTrapTime	ADD	count,count,1
		GET	tmp,rJ	
		PUSHJ	tmp+1,displaycount
		PUT	rJ,tmp
		POP 0,0

		PREFIX	:displaycount:
SevenHI	IS	#8003

top		IS	#01
mid		IS	#02
bot		IS	#04
tleft		IS	#08
bleft		IS	#10
tright		IS	#20
bright		IS	#40
dot		IS	#80

segments	BYTE	top|tleft|tright|bleft|bright|bot  	%0
		BYTE	tright|bright					%1
		BYTE	top|tright|mid|bleft|bot			%2
		BYTE	top|tright|mid|bright|bot			%3
		BYTE	tleft|tright|mid|bright			%4
		BYTE	top|tleft|mid|bright|bot			%5
		BYTE	top|tleft|mid|bleft|bright|bot		%6
		BYTE	top|tright|bright				%7
		BYTE	top|mid|bot|tleft|tright|bleft|bright	%8
		BYTE	top|mid|bot|tleft|tright|bright		%9

base		IS	$0
head		IS	$1
tail		IS	$2
code		IS	$3
bits		IS	$4
shift         IS	$5

:displaycount	SET	bits,dot<<8	%one decimal after dot
		SET	shift,0
		SET	head,:count
		GETA	base,segments
		
1H		DIV	head,head,10
		GET	tail,:rR
		LDB	code,base,tail
		SLU	code,code,shift
		OR	bits,bits,code
		ADD	shift,shift,8
		BP	head,1B
		
		SETH	base,SevenHI	%sevensegment
		STO	bits,base,0
		POP	0,0


		