;;; Macros for BK-0011/BK-0011M

.INCLUDE /k1801.inc/
.INCLUDE /bkkeys.inc/


..BASE = 140010


.MACRO	..BCM0	INS,REG
.IF NB <INS>
.IF NE ...EMT
	EMT	^O<INS>
.IFF
...A = ^O<INS>*2.+^O10+..BASE
.IF B <REG>
	JSR	PC,@<...A>
.IFF
.NTYPE	...V2,REG
.IF EQ ...V2-^O46
	MOV	@#<...A>,-(SP)
	JSR	PC,@(SP)+
.MEXIT
.ENDC
.IIF GE ...V2-6. .ERROR ; ?BKLIB-F-INVALID R E G SPECIFIED; USE RX OR -(SP)
	MOV	@#<...A>,REG
	JSR	PC,@REG
.ENDC
.ENDC
.ENDC
.ENDM

.MACRO	..BCM1	LOW,HIGH,INS,REG
.IIF NB <LOW>    .NTYPE	...V3,LOW
.IIF NB <HIGH>   .NTYPE	...V2,HIGH
.IF NB <HIGH>
 .IF EQ	...V2-^O27
  .IF IDN	<HIGH>,<#0>
   .IF B <LOW>
	CLR	R0
   .IFF
    .IF EQ ...V3-^O27
     .IF DIF <LOW>,#0
	MOV	LOW,R0
     .IFF
	CLR	R0
     .ENDC
    .IFF
	CLR	R0
	BISB	LOW,R0
    .ENDC
   .ENDC
  .IFF ; HIGH NOT #0
	MOV	HIGH*^O400,R0
   .IF NB <LOW>
    .IIF DIF <LOW>,#0	BISB	LOW,R0
   .ENDC
  .ENDC
 .IFF; HIGH NOT CONST
	MOV	HIGH,R0
	SWAB	R0
	CLRB	R0
  .IF NB <LOW>
   .IIF DIF <LOW>,#0	BISB	LOW,R0
  .ENDC
 .ENDC
.IFF; NO HIGH
 .IF NB <LOW>
  .IF EQ ...V3-^O27
   .IF IDN <LOW>,#0
	CLR	R0
   .IFF
	MOV	LOW,R0
   .ENDC
  .IFF
	CLR	R0
	BISB	LOW,R0
  .ENDC
 .ENDC
.ENDC
	..BCM0	INS,REG
.ENDM

.MACRO	..BCM2	ARG,INS,REG
.IF B <ARG>
	CLR	R0
.IFF
.IIF DIF <ARG>,R0	MOV	ARG,R0
.ENDC
	..BCM0	INS,REG
.ENDM
.MACRO	..BCM3	ARG,INS,BB,REG
.IF NB <ARG>
.IIF DIF <ARG>,R0	MOV'BB	ARG,R0
.ENDC
	..BCM0	INS,REG
.ENDM

.MACRO	..BCM4 ARG1,ARG2,INS,REG
.IF NB <ARG1>
.IF DIF <ARG1>,R0
.IF DIF <ARG1>,#0
	MOV	ARG1,R0
.IFF
	CLR	R0
.ENDC
.ENDC
.ENDC
.IF NB <ARG2>
.IF DIF <ARG2>,R1
.IF DIF <ARG2>,#0
	MOV	ARG2,R1
.IFF
	CLR	R1
.ENDC
.ENDC
.ENDC
	..BCM0	INS,REG
.ENDM

.MACRO	.KOI8	STRING,E
.IRPC	...X,<STRING>
 .IF GE ''...X-140
	.BYTE	''...X+200
 .IFF
	.BYTE	''...X
 .ENDC
.ENDR
.IIF DIF <E>,<>	.BYTE	^O<E>
.ENDM

.MACRO	.RUS	STRING,E
.IRPC	...X,<STRING>
 .IF GE ''...X-100
	.BYTE	''...X+200
 .IFF
	.BYTE	''...X
 .ENDC
.ENDR
.IIF DIF <E>,<>	.BYTE	^O<E>
.ENDM

.MACRO	.BINIT	REG
	..BCM0	0,REG
.ENDM

.MACRO	.BEXIT	REG
	.IIF NE C$B11M,	..BCM0	1,REG
	.IIF EQ C$B11M,	..BCM0	0,REG
.ENDM

.MACRO	.BTRPS	ARG,REG
	.IIF NE C$B11M,	..BCM2	ARG,2,REG
	.IIF EЙ C$B11M,	..BCM2	ARG,112,REG
.ENDM

.MACRO	.BTIM	ARG,REG
	..BCM2	ARG,3,REG
.ENDM

.MACRO	.BEMT	ARG=ON,REG
.IF IDN <ARG>,<ON>
...EMT	=	0
	..BCM0	4,REG
...EMT	=	1
.IFF
.IF IDN <ARG>,<OFF>
...EMT	=	0
	MOV	@#166,@#30
.IFF
.ERROR	;?BKLIB-F-ILLEGAL ARGUMENT ARG
.ENDC
.ENDC
.ENDM

.MACRO	.BMEM	ARG,REG
	..BCM0	10,REG
.IIF NB <ARG>	MOV	R0,ARG
.ENDM

.MACRO	.BTEST	PAGE,REG
	..BCM3	PAGE,11,B,REG
.ENDM

.MACRO	.BPAGE	PAGE,ADDR,REG
	..BCM1	PAGE,ADDR,12,REG
.ENDM

.MACRO	.BWORK	ARG,REG
	..BCM3	ARG,13,,REG
.ENDM

.MACRO	.BGETW	ADDR,REG
	..BCM3	ADDR,14,,REG
.ENDM

.MACRO	.BGETB	ADDR,REG
	..BCM3	ADDR,15,,REG
.ENDM

.MACRO	.BPUTW	ADDR,DATA,REG
	..BCM4	DATA,ADDR,16,REG
.ENDM

.MACRO	.BPUTB	ADDR,DATA,REG
	..BCM4	DATA,ADDR,17,REG
.ENDM

.MACRO	.BMOVE	AREA,REG
	..BCM3	AREA,20,,REG
.ENDM

.MACRO	.BMOVB	AREA,REG
	..BCM3	AREA,21,,REG
.ENDM

.MACRO	.BJSR	ADDR,REG
	MOV	ADDR,-(SP)
	..BCM0	22,REG
.ENDM

.MACRO	.BJMP	ADDR,REG
	MOV	ADDR,-(SP)
	..BCM0	23,REG
.ENDM

.MACRO	.BKINI	REG
	..BCM0	30,REG
.ENDM

.MACRO	.BKSET	MODE,REG
	..BCM3	MODE,31,,REG
.ENDM

.MACRO	.BKGET	ARG,REG
	..BCM0	32,REG
.IIF NB <ARG>	MOV	R0,ARG
.ENDM

.MACRO	.BTTIN	ARG,REG
	.IIF NE C$B11M,	..BCM0	33,REG
	.IIF EQ C$B11M,	..BCM0	6,REG
	.IF NB <ARG>
		.IIF DIF <ARG>,R0	MOVB	R0,ARG
	.ENDC
.ENDM

.MACRO	.BINKE	REG
	.IIF NE C$B11M,	..BCM0	34,REG
	.IIF EQ C$B11M,	..BCM0	102,REG
.ENDM

.MACRO	.BGLIN	ADDR,LEN,REG
	..BCM4	ADDR,LEN,35,REG
.ENDM

.MACRO	.BEDIT	ADDR,LEN,REG
	..BCM4	ADDR,LEN,36,REG
.ENDM

.MACRO	.BKRES	REG
	..BCM0	37,REG
.ENDM

.MACRO	.BSFUN	KEY,ADDR,REG
	..BCM4	KEY,ADDR,40,REG
.ENDM

.MACRO	.BGFUN	KEY,ADDR,REG
	..BCM4	KEY,ADDR,41,REG
.ENDM

.MACRO	.BTINI	REG
	..BCM0	50,REG
.ENDM

.MACRO	.BTSET	MODE,REG
	..BCM3	MODE,51,,REG
.ENDM

.MACRO	.BTGET	ARG,REG
	..BCM0	52,REG
.IF NB <ARG>
.IIF DIF <ARG>,R0	MOV	R0,ARG
.ENDC
.ENDM

.MACRO	.BSCOL	FORE,BACK,REG
	..BCM1	BACK,FORE,53,REG
.ENDM

.MACRO	.BGCOL	REG
	..BCM0	54,REG
.ENDM

.MACRO	.BPAL	ARG,REG
	..BCM3	ARG,55,B,REG
.ENDM

.MACRO	.BBUF	WRITE,VIDEO,REG
	..BCM1	VIDEO,WRITE,56,REG
.ENDM

.MACRO	.BSPOS	LINE,POS,REG
	.IIF NE C$B11M,	..BCM1	POS,LINE,57,REG
	.IIF EQ C$B11M,	..BCM1	POS,LINE,24,REG
.ENDM

.MACRO	.BPOS	LINE,POS,REG
	MOV	#LINE*400+POS,R0
	..BCM0	57,REG
.ENDM

.MACRO	.BGPOS	REG
	.IIF NE C$B11M,	..BCM0	60,REG
	.IIF EQ C$B11M,	..BCM0	26,REG
.ENDM

.MACRO	.BSCRL	ARG,REG
	.IIF NE C$B11M,	..BCM3	ARG,61,,REG
	.IIF EQ C$B11M,	..BCM3	ARG,110,,REG
.ENDM

.MACRO	.BCLS	REG
	..BCM0	62,REG
.ENDM

.MACRO	.BTOUT	CHAR,REG
	.IIF NE C$B11M,	..BCM3	CHAR,63,B,REG
	.IIF EQ C$B11M,	..BCM3	CHAR,16,B,REG
.ENDM

.MACRO	.BPRIN	ADDR,REG
	.IIF NE C$B11M,	..BCM3	ADDR,64,,REG
	.IIF EQ C$B11M,	..BCM3	ADDR,20,,REG
.ENDM

.MACRO	.BSTR	ADDR,REG
	..BCM3	ADDR,65,,REG
.ENDM

.MACRO	.BSGRF	X,Y,REG
	..BCM4	X,Y,66,REG
.ENDM

.MACRO	.BGGRF	X,Y,REG
	..BCM0	67,REG
	.IF NB <X>
		.IIF DIF <X>,R0	MOV	R0,X
	.ENDC
	.IF NB <Y>
		.IIF DIF <Y>,R1	MOV	R1,Y
	.ENDC
.ENDM

.MACRO	.BSCGR	COLOR,REG
	..BCM3	COLOR,70,B,REG
.ENDM

.MACRO	.BGCGR	ADDR,REG
	..BCM0	71,REG
	.IF NB <ADDR>
		.IIF DIF <ADDR>,R0	MOV	R0,ADDR
	.ENDC
.ENDM

.MACRO	.BSMOD	MODE,REG
	..BCM3	MODE,72,B,REG
.ENDM

.MACRO	.BSPNT	X,Y,REG
	..BCM4	X,Y,73,REG
.ENDM

.MACRO	.BGPNT	X,Y,REG
	..BCM4	X,Y,74,REG
.ENDM

.MACRO	.BVECT	X,Y,REG
	..BCM4	X,Y,75,REG
.ENDM

.MACRO	.BRECT	X,Y,REG
	..BCM4	X,Y,76,REG
.ENDM

.MACRO	.BCIRC	ADDR,REG
	..BCM3	ADDR,77,,REG
.ENDM

.MACRO	.BFILL	COLOR,REG
	..BCM3	COLOR,100,B,REG
.ENDM

.MACRO	.BSOUN	LEN,TONE,REG
	..BCM4	LEN,TONE,101,REG
.ENDM

.MACRO	.BSWIN	ADDR,REG
	..BCM3	ADDR,102,,REG
.ENDM

.MACRO	.BGWIN	ADDR,REG
	..BCM3	ADDR,103,,REG
.ENDM

.MACRO	.BSTYP	ARG,REG
	..BCM3	ARG,105,REG
.ENDM

.MACRO	.BGTYP	ARG,REG
	..BCM0	106,REG
	.IF NB <ARG>
		.IIF DIF <ARG>,R0	MOV	R0,ARG
	.ENDC
.ENDM

.MACRO	.BGOSE	MODE,REG
	..BCM3	MODE,107,B,REG
.ENDM

.MACRO	.BGOUT	CHAR,REG
	..BCM3	CHAR,110,B,REG
.ENDM

.MACRO	.BMOT	ARG,REG
	..BCM2	ARG,114,REG
.ENDM

.MACRO	.BMB10	AREA,REG
	..BCM3	AREA,115,,REG
.ENDM

.MACRO	.BPINI	REG
	..BCM0	124,REG
.ENDM

.MACRO	.BPSET	MODE,REG
	..BCM3	MODE,125,,REG
.ENDM

.MACRO	.BPGET	ARG,REG
	..BCM0	126,REG
	.IIF NB <ARG>	MOV	R0,ARG
.ENDM

.MACRO	.BPOUT	CHAR,REG
	..BCM3	CHAR,127,B,REG
.ENDM

.MACRO	.BPRDY	ARG,REG
	..BCM0	130,REG
	.IIF NB <ARG>	MOVB	R0,ARG
.ENDM

.MACRO	.BPPOS	ARG,REG
	..BCM0	131,REG
.IIF NB <ARG>	MOV	R0,ARG
.ENDM

.MACRO	.EXIT
	.BEXIT
.ENDM

.MACRO	.PRINT	addr
	.BPRIN	addr
.ENDM

.MACRO	.TTINR	ch
	.BINKE	ch
.ENDM

.MACRO	.TTYIN	ch
	.BTTIN	ch
.ENDM

.MACRO	.TTYOUT	ch
	.BTOUT	ch
.ENDM
