
						ORG 50000
						jp		set_up_the_screen			; org    setup screen and draw the cards
						jp		drawcards					; org+3  for redrawing the cards
						jp		colour_cards				; org+6  colour according to string a$
						jp 		colour_cards_25				; org+9  colour according to NEXT 25 chars of string a$
						jp	    draw_just_one_card			; org+12 draw just one card
cardtodraw:				defb	0							; org+15  variable - the card to draw
						jp		colour_cards_b				; org+16	;placeholder for DUETS
						jp		colour_cards_b_50			; org+19	;placeholder for DUETS
						jp		sfx1						; org+22	;sfx1
						jp		sfx2						; org+25	;sfx2
						jp		testlowbit					; org+28    tests lowest bit of totest = returns 1/0
totest:					defb	0							; org+31	number to test
						jp		printword					; org+32	prints one word
wordtoprint:			defw	270							; org+35/36	variable word to print
						jp		printallwords				; org+37   prints all the words for board
						

colour_cards_b:												; will be useful to write these in DUETS
colour_cards_b_50:											; 


;------------
;prints all 25 words for cards using my nice 6-pixel wide font 
;words are in the dim array r(25)
;---------------------------------

printallwords:			
						ld		hl,(5C4Bh)			;VARS
						ld		de,2+100+5+11		;first one in r(a)
						add		hl,de
						ld		b,25
movetonextword:
						ld		e,(hl)			; add five for next one
						inc		hl
						ld		d,(hl)
						inc		hl
						inc		hl
						inc		hl
						inc		hl				; hl is set up for next one
						push	hl
						push	bc
						ex		de,hl
						dec		hl
												; needs to be placed at right place on screen
						push	hl
						ld		a,26
						sub		b				; a=1 TO 25
						call	findprintcoordsforcard	; gets card co-ord for card a

						ld		a,l
						cp		01h
						jr		z,leftmost
						add		a,3
						cp		18h
						jr		nz,middle
						add		a,3
						inc		a
middle:						
leftmost:						

						ld		l,a

						inc		h
						inc		l

						ld		(xcoord),hl		; where the print routine prints

						pop		hl
									
						call 	printwordpopin
						pop		bc
						pop		hl
						djnz	movetonextword
						ret			

;---------------------------
;prints a word using my nice 6pixel wide font 
;word is in the SECOND defined (two-byte) BASIC variable let mx=100:let l=usr org+32
;---------------------------------
printword:
						ld		hl,(5C4Bh)			;VARS
						ld		de,2+100+5
						add		hl,de
						ld		e,(hl)
						inc		hl
						ld		d,(hl)
						ex		de,hl						
						dec		hl					; down by 1
printwordpopin:			push 	hl
						or		a					; clear cary flag
						add		hl,hl
						add		hl,hl
						add		hl,hl				; x8
						pop		de
						add		hl,de
						add		hl,de
						add		hl,de				; x11
						
						ld		de,50820
						add		hl,de				; hl now points at word
						ld		(64820),hl			; string to print smc
						ld		hl,11	
						ld		(64823),hl			; length of string smc
						call	64819				; print string
						ret	

;---------------------
;test low bit od totest variable - sets BC (and thus return value to basic) as the lowest bit
;dvides totest by 2, thus can be used repeatedly to test each bit in turn
;thus bit 0 = afterdark, bit 1 = original, bit 2 = duets
;AND the resultant 01000 should be in (totest) if it is valid
;---------------------

testlowbit:
						ld		a,(totest)
						ld		bc,0
						or		a						; clear carry flag
						rra								; sets carry flag and divides by 2 (bit 7 becomes 0)
						ld		(totest),a				;
						ret		nc						; return 0 if lowest bit wasn't set 
						ld		bc,1
						ret								; returns 1 if lowest bit was set

;-----------------------------------
;SFX
;-----------------------------------
sfx1;							; ORG+25
       					   ;sound_setup			;"qukkkkk" sound
		ld de, 5						;
		ld b,3
		ld hl, 938 						; pitch
sound_loop:
		dec h
		push bc
		push de
		push hl
		call 949 						; call ROM beeper routine
		pop hl
		pop de
		pop bc
		djnz sound_loop
		ret
sfx2:
		ld de,10						;
		ld b,9
		ld hl,20						; pitch
sound_loop2:
		inc h
		push bc
		push de
		push hl
		call 949 						; call ROM beeper routine
		pop hl
		pop de
		pop bc
		dec de
		djnz sound_loop2

		ret



;                        ld      a,2              ; Initially printing to the Upper screen
;                        call    5633             ; Open channel
;                        ld      a,0              ; 0 is the code for blue
;                        call    8859             ; set border colour.
;                        ld      a,8*0+6          ; yellow ink, black paper
;                        ld      (23693),a        ; set our screen colours.

;                        call    3503             ; clear the screen.
                        
;						ld		de,UDGchars	     ;setup where the UDGS are stored
;						ld		(23675),de


;						ld		a,25
;						ld		(cardtodraw),a
;						call	draw_just_one_card


;						ret
;
;						call    set_up_the_screen
;						call	colour_cards					; blast out the board colours
;						call	colour_cards_25					; same but the whole set 

;colour_just_one_card:
;						call	draw_just_one_card
;						ld		a,(cardcounter)
;						call	findattrforcard
						
;						;										THIS FELLOW TO DO



draw_just_one_card:
						ld		a,(cardtodraw)
						call	findprintcoordsforcard
						ld		a,22
						rst		10h
						ld		a,h
						rst		10h
						ld		a,l
						rst		10h
						ld		a,21
						rst		10h
						ld      a,0
						rst 	10h
						push	hl
						call	cardtops
						pop		hl
						inc		h
						ld		a,22
						rst		10h
						ld		a,h
						rst		10h
						ld		a,l
						rst		10h
						call	cardbots						
						ret

cardcounter:			defb	1						
						


				
				
				
colour_cards:								; will need to be hl,(5C4Bh)	; system variable VARS				
											; will need to be 2
						ld		hl,(5C4Bh)
						ld		de,2
						add		hl,de
						ld		(colour_card_smc+1),hl
						jr		colour_cardscommon			
colour_cards_25:		ld		hl,(5C4Bh)						; will need to be hl,(5C4Bh)	; system variable VARS	
						ld		de,27							; will need to be 27
						add		hl,de
						ld		(colour_card_smc+1),hl						
						
colour_cardscommon:
						ld		a,1						; start with card 1
enterhere:				ld		(cardwereworkingon),a
colour_cards_loop:		ld		a,(cardwereworkingon)	; get the screen attr address for card we're on
						cp		25
						jr		z,lastcard
						call	findattrforcard
						push	hl
						
colour_card_smc:		ld		hl,0					; work out where in the string we are ****
						
						ld		a,(cardwereworkingon)
						ld		e,a
						ld		d,0
						add		hl,de
						
						ld		a,(hl)					; get the colour
						
						pop		hl
						call	revealcard

						ld		a,(cardwereworkingon)
						inc		a
						cp		26
						ret		z
						ld		(cardwereworkingon),a
						jr		colour_cards_loop

cardwereworkingon:		defb	0
						
lastcard:				ld		a,26
						call	findattrforcard
						push	hl
						jr		colour_card_smc

;-----------------------------------
;PRINT the BASIC variable a$
;-----------------------------------

;PRINTHESTRING:
					
;						ld		hl,(5C4Bh)				; VARS system varaible
;						ld		de,3
;						add		hl,de					; add 3, should be the 
														; text of a$, first str$												
;						call	prop_string
;						call	screenflash
;						ret





;---------------------------------------------------------------------------------
;set_up_the_screen, including border and the stream
;---------------------------------------------------------------------------------
set_up_the_screen:
                        ld      a,2              ; Initially printing to the Upper screen
                        call    5633             ; Open channel
                        ld      a,0              ; 0 is the code for blue
                        call    8859             ; set border colour.
                        ld      a,8*0+6          ; yellow ink, black paper
                        ld      (23693),a        ; set our screen colours.

                        call    3503             ; clear the screen.
                        

						ld		de,UDGchars	     ;setup where the UDGS are stored
						ld		(23675),de

						call	drawcards
						ret		        					        
								        

colourcard				; colour in some cards
				
						
bigloop:											; make it flash!
						ld		de,3000h

loop:						
						ld		a,(cardnumber)
						dec		a
						jr		nz,notreseta
						ld		a,25					
notreseta:				
						ld		(cardnumber),a				
						push	de
						call	findattrforcard
						pop		de
						ld		a,(de)
						and		%1000111
						push	de
						call	revealcard
						pop		de
						dec		de
						ld		a,d
						or		e
						cp		1
						jr		nz,loop
						
						;jr		bigloop				cycle colours

						ret
cardnumber:				defb	25
						
						
;colourcard - colour a 'card' colour determined by 'a'
;two versions, one that colours it in, but hides the word
;one that reveals the word


norevealcard:						
						push	de
						push	bc
						ld		b,10				; counter
topcardloop:			ld		(hl),a
						inc		hl
						djnz	topcardloop
						ld 		de,32-10
						add		hl,de
						ld		(hl),a
						inc		hl
						ld		c,a				; save ink colour
						rla
						rla
						rla						; ink becomes paper
						or		c
						ld		b,8				; counter
botcardloop:			ld		(hl),a
						inc		hl
						djnz	botcardloop
						ld		a,c
						ld		(hl),a
						pop		bc
						pop		de				
						ret

revealcard:						
						push	de
						push	bc
						ld		b,10				; counter
topcardloop2:			ld		(hl),a
						inc		hl
						djnz	topcardloop2
						ld 		de,32-10
						add		hl,de
						ld		b,10				; counter
botcardloop2:			ld		(hl),a
						inc		hl
						djnz	botcardloop2
						pop		bc
						pop		de
						
						ret
	
drawcards:				
						call	printat				; cards start at top left
						defb	0,0
						
						ld		b,8					; 8 rows of cards
nextcardrow:
						push	bc
						call	cardrow
						pop		bc
						djnz	nextcardrow
						
						call	printat				; sets up print positon 
						defb	16,11				; using data after it
						call	cardtops			; for the 25th card
						call	printat
						defb	17,11
						call	cardbots						
						
						ret	
						
printat:				ld		a,22				; sets up print position to the coordinates directly after the call
						rst		10h
						pop		hl
						ld		a,(hl)
						rst		10h
						inc		hl
						ld		a,(hl)
						rst		10h
						inc		hl
						push	hl
						ret
						
																
cardrow:
						ld		a,32
						rst		16
						call	cardtops
						call	cardtops
						call	cardtops						
						ld		a,32
						rst		16
						ld		a,32
						rst		16
						call	cardbots
						call	cardbots
						call	cardbots						
						ld		a,32
						rst		16						
						ret					

cardtops:				ld		a,90h
						rst 	10h        
						ld		b,8
innerloopcardtop:		ld 		a,91h
						rst		10h
						djnz	innerloopcardtop
						ld		a,92h
						rst		10h
						ret

cardbots:				ld		a,93h
						rst 	10h        
						ld		b,8
innerloopcardbot:		ld 		a,94h
						rst		10h
						djnz	innerloopcardbot
						ld		a,95h
						rst		10h
						ret


;called with a=position (1 to 25)
;returns with HL pointing at ATTR co-ords
findattrforcard:
						ld		hl,5800h+1				;starting point on screen attr
						cp		25						;last card is special case
						jr		nz,findattforcardloop
						ld		de,10+8*64
						add		hl,de
						ret
findattforcardloop:		dec		a
						ret		z
						ld		de,10
						add		hl,de
						dec		a
						ret		z
						add		hl,de
						dec		a
						ret		z
						ld		de,32+10+2
						add		hl,de
						jr		findattforcardloop

;called with a=position (1 to 25)
;returns with HL pointing at print at co-ords						
findprintcoordsforcard:									; find print co-ords
						or		a						; clear carry flag
						rla
						ld		e,a
						ld		d,0
						ld		hl,lookupposition-2
						add		hl,de
						ld		a,(hl)
						ld		e,a
						inc		hl
						ld		a,(hl)
						ld		d,a
						ex		de,hl
						ret
lookupposition:			
						defw	0001h,000bh,0015h
						defw	0201h,020bh,0215h
						defw	0401h,040bh,0415h
						defw	0601h,060bh,0615h
						defw	0801h,080bh,0815h	
						defw	0A01h,0A0bh,0A15h	
						defw	0C01h,0C0bh,0C15h	
						defw	0E01h,0E0bh,0E15h			
						defw	100bh



; codenames After Dark word set (only 390 words) - annoyingly THERE are repeats (EG HOTEL)
; max word length = 11
; 390*11=4180 bytes

org 50820

defb "ACID       ","BRA        ","CLAM       ","DOMINATE   ","FRENCH     ","HEADLIGHTS ","KITTY      ","MEMBER     ","PAYBACK    ","PUSSY      ","SERVICE    ","STRAIGHT   ","TRIGGER    ","VODKA      "
defb "ADDICT     ","BRAND      ","CLAP       ","DONKEY     ","FREUD      ","HEAT       ","KNOB       ","MISSIONARY ","PECKER     ","QUEEF      ","SHAFT      ","STRAP      ","TRIM       ","VULGAR     "
defb "ADHESIVE   ","BRAZILIAN  ","CLEVELAND  ","DOORNAIL   ","FRICTION   ","HERB       ","KNOCKERS   ","MIXER      ","PEE        ","QUEER      ","SHALLOW    ","STRETCH    ","TRUNK      ","WAD        "
defb "ANAL       ","BREAST     ","CLUB       ","DOUCHE     ","FROTTAGE   ","HERRING    ","LASH       ","MOIST      ","PERIOD     ","RACK       ","SHAME      ","STRIP      ","TUB        ","WAFFLE     "
defb "ANTEATER   ","BROWNIE    ","COCK       ","DRAG       ","FRUIT      ","HIGH       ","LATEX      ","MOM        ","PERVERT    ","RAG        ","SHAVE      ","STUD       ","TUMOR      ","WANK       "
defb "ASS        ","BUGGER     ","COCKTAIL   ","DRAPES     ","FUCK       ","HO         ","LEAK       ","MOOB       ","PICKLE     ","RAGTIME    ","SHEATH     ","STUMP      ","TUNA       ","WASTE      "
defb "BAG        ","BULLDOG    ","COIL       ","DRUNK      ","FURRY      ","HOG        ","LEGS       ","MOTEL      ","PIGLET     ","RAVE       ","SHEEP      ","SWALLOW    ","TWIG       ","WAX        "
defb "BAKED      ","BUMP       ","COMMANDO   ","EAT        ","G-SPOT     ","HOLE       ","LEXINGTON  ","MOTORBOAT  ","PILLOWS    ","RAW        ","SKANK      ","SWEAT      ","UDDERS     ","WEED       "
defb "BALLS      ","BURN       ","CONDOM     ","EMISSION   ","GAG        ","HOMERUN    ","LICK       ","MOUTH      ","PIMP       ","REAM       ","SKIN       ","SWELL      ","URANUS     ","WET        "
defb "BANG       ","BUSH       ","COTTAGE    ","EMPTY      ","GANG       ","HOOKER     ","LINE       ","MOVIE      ","PING-PONG  ","REAPER     ","SKIRT      ","TABOO      ","VEGAS      ","WHACK      "
defb "BAZOOKA    ","BUST       ","COUGAR     ","ESCORT     ","GAPE       ","HOOP       ","LINGERIE   ","MUCK       ","PINK       ","RECORDING  ","SMACK      ","TEABAG     ","VEIN       ","WHISKEY    "
defb "BEAR       ","BUTT       ","COWGIRL    ","EXPERIMENT ","GARGLE     ","HORNY      ","LINGUS     ","MURDER     ","PIPE       ","RECTUM     ","SMEGMA     ","TEASE      ","VIBRATOR   ","WIDOW      "
defb "BEAVER     ","CABLE      ","COYOTE     ","FACIAL     ","GASH       ","HUMP       ","LIPS       ","MUTILATION ","PITCHER    ","RED        ","SNATCH     ","TENT       ","VIDEO      ","WINE       "
defb "BEEF       ","CAGE       ","CRABS      ","FAKE       ","GERBIL     ","HYPODERMIC ","LIQUOR     ","NAKED      ","PLAYER     ","REGRET     ","SNORT      ","TEQUILA    ","VINYL      ","WITHDRAWAL "
defb "BEER       ","CANCER     ","CRACK      ","FATTY      ","GIGOLO     ","INCH       ","LIZARD     ","NECKLACE   ","PLOW       ","RESTRAINT  ","SNOWBALL   ","TERMINAL   ","VIRGIN     ","YMCA       "
defb "BEHIND     ","CANDLE     ","CRAP       ","FECAL      ","GLIDE      ","INFECTION  ","LOBSTER    ","NIPPLE     ","PLUMS      ","REVENGE    ","SOLO       ","THREESOME  "
defb "BENT       ","CANDYMAN   ","CREAM      ","FELCH      ","GLORY      ","IPA        ","LOOSE      ","NOODLE     ","PONY       ","ROD        ","SORE       ","THROAT     "
defb "BITCH      ","CANNONS    ","CROOKED    ","FETISH     ","GLUMPKIN   ","ITCH       ","LOTION     ","NUDE       ","PORK       ","ROGER      ","SPANK      ","TICKLE     "
defb "BITE       ","CARPET     ","CUFFS      ","FILTH      ","GOBBLE     ","JACUZZI    ","LUBE       ","NURSE      ","PORN       ","RUB        ","SPEED      ","TIP        "
defb "BLONDE     ","CASH       ","CURTAINS   ","FINGER     ","GORGONZOLA ","JAZZ       ","LUST       ","NUTS       ","POT        ","RUG        ","SPERM      ","TIT        "
defb "BLOOD      ","CASTRATE   ","DADDY      ","FIST       ","GRASS      ","JERK       ","MAGGOT     ","NYLON      ","POUND      ","RUNNY      ","SPIRITS    ","TONGUE     "
defb "BLOW       ","CATCHER    ","DAME       ","FLANGE     ","GREASE     ","JOB        ","MAMMARY    ","OBSCENITY  ","PRICK      ","SALAD      ","SPREAD     ","TOOL       "
defb "BLUE       ","CENTIMETER ","DEEP       ","FLASH      ","GRIND      ","JOHN       ","MANDINGO   ","OEDIPUS    ","PRISON     ","SALTY      ","SPRINKLE   ","TOP        "
defb "BLUSH      ","CHAINS     ","DEPARTED   ","FLAY       ","GROPE      ","JOHNSON    ","MANTIS     ","OOZE       ","PROSTATE   ","SANCHEZ    ","SQUIRT     ","TORTURE    "
defb "BONDAGE    ","CHAPS      ","DICK       ","FLESH      ","GROUP      ","JOINT      ","MARE       ","ORAL       ","PROSTITUTE ","SAUCE      ","STEAMY     ","TOSS       "
defb "BONE       ","CHEDDAR    ","DILDO      ","FLOG       ","GROWL      ","JUGS       ","MARMALADE  ","ORGASM     ","PRURIENT   ","SAUNA      ","STIFF      ","TOUCH      "
defb "BONG       ","CHEEK      ","COUPLE     ","FLOWER     ","GROWTH     ","JUICE      ","MARTINI    ","ORIFICE    ","PUB        ","SCAB       ","STILETTO   ","TOWEL      "
defb "BOOB       ","CHOKE      ","DOCK       ","FLUFF      ","GUSH       ","JUNG       ","MEASURE    ","OVERDOSE   ","PUCKER     ","SCORE      ","STINK      ","TOY        "
defb "BOOTY      ","CIGAR      ","DODO       ","FLUSH      ","HAMSTER    ","KIDNAP     ","MEAT       ","OYSTER     ","PUFF       ","SCREW      ","STONES     ","TRAMP      "
defb "BOTTOM     ","CIGARETTE  ","DOGGY      ","FORESKIN   ","HEADBOARD  ","KINK       ","MELONS     ","PACKAGE    ","PURPLE     ","SEED       ","STOOL      ","TRIATHLON  "


; Codenames Word Set (base game 400 words)			
			org 	55000 		; 55000 to 59399

defb "HOLLYWOOD  ","WELL       ","FOOT       ","NEW YORK   ","SPRING     ","COURT      ","TUBE       ","POINT      ","TABLET     ","SLIP       ","DATE       ","DRILL      ","LEMON      ","BELL       "
defb "SCREEN     ","FAIR       ","TORCH      ","STATE      ","MATCH      ","IRON       ","BLOCK      ","FRANCE     ","AUSTRALIA  ","LIMOUSINE  ","STREAM     ","GLOVE      ","NURSE      ","LEPRECHAUN "
defb "PLAY       ","TOOTH      ","ARM        ","BERMUDA    ","DIAMOND    ","WHALE      ","COMIC      ","MAMMOTH    ","GREEN      ","PASS       ","MISSILE    ","PASTE      ","DROP       ","PHEONIX    "
defb "MARBLE     ","STAFF      ","FIGURE     ","PARK       ","CENTAUR    ","SHADOW     ","FISH       ","COTTON     ","EGYPT      ","THEATRE    ","SCALE      ","FALL       ","TRACK      ","FORCE      "
defb "DINOSAUR   ","BILL       ","MINE       ","TURKEY     ","MARCH      ","CONTRACT   ","BRIDGE     ","ROBIN      ","LINE       ","PLATE      ","BAND       ","FIRE       ","BANK       ","BOOM       "
defb "CAT        ","SHOT       ","SUIT       ","CHOCOLATE  ","ROULETTE   ","MERCURY    ","MOON       ","NET        ","LAWYER     ","SATELLITE  ","ANGEL      ","SPIDER     ","GERMANY    ","FORK       "
defb "PITCH      ","KING       ","CRANE      ","TRIP       ","DOG        ","CONDUCTOR  ","PART       ","BUGLE      ","WITCH      ","KETCHUP    ","PRESS      ","SPINE      ","WORM       ","ALPS       "
defb "BOND       ","PAN        ","BEIJING    ","RACKET     ","CROSS      ","SEAL       ","AZTEC      ","MAPLE      ","PARACHUTE  ","HOTEL      ","BERRY      ","SOLDIER    ","RAY        ","POST       "
defb "GREECE     ","SQUARE     ","MASS       ","BAT        ","WAVE       ","CAR        ","SMUGGLER   ","ENGLAND    ","CRASH      ","TAIL       ","CARD       ","HORN       ","CAPITAL    ","FENCE      "
defb "DECK       ","BUFFALO    ","MICROSCOPE ","JET        ","DUCK       ","RING       ","TRAIN      ","FIELD      ","GOLD       ","TICK       ","CHECK      ","QUEEN      ","STRIKE     ","KANGAROO   "
defb "SPIKE      ","SCIENTIST  ","ENGINE     ","SHAKESPEARE","WIND       ","KID        ","EMBASSY    ","ROBOT      ","NOTE       ","GROUND     ","DRAFT      ","HAM        ","WAR        ","MOUSE      "
defb "CENTER     ","CHICK      ","CHINA      ","BOLT       ","SPOT       ","PIANO      ","PUPIL      ","PLOT       ","LION       ","POLICE     ","HEAD       ","LITTER     ","CONCERT    ","MUG        "
defb "VACUUM     ","ATLANTIS   ","STRAW      ","SWITCH     ","SKYSCRAPER ","LASER      ","SCUBA DIVER","AFRICA     ","PLASTIC    ","DWARF      ","LAP        ","LIFE       ","HONEY      ","HORSESHOE  "
defb "UNICORN    ","SPY        ","PANTS      ","WALL       ","PAPER      ","SOUND      ","ICE        ","TAG        ","WEB        ","FAN        ","ORANGE     ","TEMPLE     ","CANADA     ","SCORPION   "
defb "UNDERTAKER ","MAIL       ","EUROPE     ","SOUL       ","APPLE      ","POLE       ","TAP        ","MOUTH      ","AMBULANCE  ","DRESS      ","ICE CREAM  ","RABBIT     ","BUCK       ","AGENT      "
defb "SOCK       ","NUT        ","BOOT       ","GHOST      ","OIL        ","SUPERHERO  ","CODE       ","KIWI       ","HOSPITAL   ","SATURN     ","FILM       ","BUTTON     ","SNOWMAN    ","HELICOPTER "
defb "LOCH NESS  ","LOG        ","PRINCESS   ","TIME       ","COOK       ","REVOLUTION ","SHOE       ","MOLE       ","SPELL      ","GRASS      ","WASHER     ","GAME       ","BEAT       ","HOLE       "
defb "HORSE      ","PIRATE     ","LINK       ","DANCE      ","FLY        ","PIT        ","SERVER     ","SCHOOL     ","LOCK       ","BRUSH      ","POOL       ","STAR       ","JAM        ","ORGAN      "
defb "BERLIN     ","FACE       ","LUCK       ","AMAZON     ","CAST       ","GAS        ","CLUB       ","SINK       ","WATER      ","CHAIR      ","SHARK      ","JUPITER    ","COPPER     ","JACK       "
defb "PLATYPUS   ","STICK      ","OLIVE      ","GRACE      ","BEAR       ","GLASS      ","ROW        ","PISTOL     ","LONDON     ","ROCK       ","VAN        ","VET        ","BEACH      ","CHARGE     "
defb "PORT       ","DISEASE    ","PALM       ","MOSCOW     ","PIN        ","WASHINGTON ","PYRAMID    ","OPERA      ","CASINO     ","PILOT      ","STRING     ","NIGHT      "
defb "CHEST      ","YARD       ","TEACHER    ","PUMPKIN    ","THIEF      ","BARK       ","BUG        ","MINT       ","CYCLE      ","TELESCOPE  ","CALF       ","AIR        "
defb "BOX        ","MOUNT      ","THUMB      ","ANTARCTICA ","TRUNK      ","SNOW       ","PENGUIN    ","ROOT       ","BAR        ","FILE       ","HAWK       ","BATTERY    "
defb "COMPOUND   ","SLUG       ","OCTOPUS    ","WHIP       ","AMERICA    ","IVORY      ","POUND      ","SUB        ","CLIFF      ","LAB        ","EAGLE      ","GENIUS     "
defb "SHIP       ","DICE       ","HOOD       ","HEART      ","NOVEL      ","PIPE       ","HIMALAYAS  ","CROWN      ","ROUND      ","INDIA      ","NEEDLE     ","SHOP       "
defb "WATCH      ","LEAD       ","TIE        ","TABLE      ","CELL       ","COVER      ","CZECH      ","BACK       ","BOMB       ","RULER      ","FOREST     ","BOTTLE     "
defb "SPACE      ","HOOK       ","DOCTOR     ","BALL       ","BOW        ","DEGREE     ","ROME       ","PLANE      ","GIANT      ","NAIL       ","DRAGON     ","STADIUM    "
defb "FLUTE      ","CARROT     ","WAKE       ","FIGHTER    ","MODEL      ","TOKYO      ","EYE        ","MEXICO     ","HAND       ","SWING      ","KEY        ","ALIEN      "
defb "TOWER      ","POISON     ","CRICKET    ","COLD       ","KNIFE      ","CHURCH     ","BOARD      ","CLOAK      ","NINJA      ","OLYMPUS    ","BELT       ","LIGHT      "
defb "DEATH      ","STOCK      ","MILLIONAIRE","DAY        ","KNIGHT     ","PIE        ","BED        ","CIRCLE     ","ROSE       ","CHANGE     ","CAP        ","TRIANGLE   "								


;duet word set (400 words)
		;55000+4400 (59400)
defb "DRUM       ","BRIDE      ","WAGON      ","UNIVERITY  ","HIT        ","ASH        ","BASS       ","ASTRONAUT  ","DOLL       ","NERVE      ","COACH      ","BEAM       ","SPOON      ","COUNTRY    "
defb "NOSE       ","KING ARTHUR","STAMP      ","CAMP       ","BRAIN      ","LEAF       ","TUTU       ","COAST      ","LUNCH      ","THUNDER    ","POTATO     ","DESK       ","ONION      ","ELEPHANT   "
defb "ANCHOR     ","COWBOY     ","FLOOD      ","MOHAWK     ","SANTA      ","PITCHER    ","BARBECUE   ","LEATHER    ","SKATES     ","MUSKETEER  ","SNAP       ","SADDLE     ","GENIE      ","MARK       "
defb "SHOULDER   ","GOVERNOR   ","MANICURE   ","ANTHEM     ","HALLOWEEN  ","NEWTON     ","BALLOON    ","FIDDLE     ","CRAFT      ","GLACIER    ","CAKE       ","RAT        ","TANK       ","BLIND      "
defb "SPIRIT     ","CABLE      ","SWAMP      ","EINSTEIN   ","HIDE       ","CRYSTAL    ","GEAR       ","KISS       ","PEW        ","POWDER     ","TURTLE     ","BACON      ","SHERLOCK   ","SQUASH     "
defb "BOOK       ","RAZOR      ","DRESSING   ","BRICK      ","BRAZIL     ","TEAR       ","STABLE     ","BIKINI     ","PEN        ","ROLL       ","CHRISTMAS  ","RUBBER     ","BAY        ","MOTHER     "
defb "KICK       ","FOG        ","RADIO      ","CRAB       ","CONE       ","SKULL      ","WHEELCHAIR ","EGG        ","BUTTER     ","WEREWOLF   ","CHERRY     ","PATIENT    ","DRYER      ","DRAWING    "
defb "BOSS       ","FEVER      ","BANANA     ","POLISH     ","KNOT       ","PAINT      ","STORM      ","GOLDILOCKS ","PILLOW     ","CHAIN      ","MOSES      ","SAW        ","BROTHER    ","RAIL       "
defb "ROPE       ","STREET     ","PAD        ","CAPTAIN    ","WISH       ","AXE        ","SHORTS     ","POPCORN    ","CASTLE     ","SECOND     ","TEAM       ","OASIS      ","MESS       ","MISS       "
defb "AVALANCHE  ","TEXAS      ","SUN        ","LETTER     ","RUST       ","WING       ","STEEL      ","EAR        ","SCROLL     ","BUNK       ","CANE       ","VENUS      ","LADDER     ","PURSE      "
defb "SHEET      ","NAPOLEON   ","SUGAR      ","DIRECTOR   ","ACE        ","SCRATCH    ","BUCKET     ","CAESAR     ","DISK       ","BEARD      ","BULB       ","BENCH      ","SCARECROW  ","IGLOO      "
defb "TUXEDO     ","EARTH      ","RAM        ","SISTER     ","BREAD      ","RECORD     ","DASH       ","GREENHOUSE ","DRONE      ","STEAM      ","BISCUIT    ","RIP        ","NOTRE DAME ","LIP        "
defb "SHAMPOO    ","CHEESE     ","SACK       ","MOUNTIE    ","SUMO       ","SAHARA     ","WALRUS     ","DUST       ","HAMMER     ","CLOUD      ","SPRAY      ","ST.PATRICK ","KILT       ","MONKEY     "
defb "FROG       ","DENTIST    ","RAINBOW    ","WHISTLE    ","REINDEER   ","KITCHEN    ","LEMONADE   ","SLIPPER    ","FLOOR      ","VALENTINE  ","PEPPER     ","ROAD       ","SHED       ","BOWLER     "
defb "MILK       ","WHEEL      ","MAGAZINE   ","BRASS      ","TEA        ","HELMET     ","FLAG       ","TROLL      ","JAIL       ","STICKER    ","PUPPET     ","CHALK      ","BONSAI     ","SWEAT      "
defb "GANGSTER   ","BUTTERFLY  ","STORY      ","SALAD      ","ARMOR      ","SMOKE      ","CAVE       ","QUACK      ","BREAK      ","SNAKE      ","MILL       ","GYMNAST    ","WONDERLAND ","DRIVER     "
defb "SPURS      ","ZOMBIE     ","PIG        ","CLEOPATRA  ","TOAST      ","PENNY      ","ANT        ","VOLUME     ","LACE       ","BATTLESHIP ","MARACAS    ","METER      ","SLING      ","DELTA      "
defb "STEP       ","JOAN OF ARC","COMET      ","BATH       ","POLO       ","GUM        ","VAMPIRE    ","SKI        ","POCKET     ","BATTLE     ","FOAM       ","RODEO      ","SQUIRREL   ","SALT       "
defb "MUMMY      ","BLACKSMITH ","CHIP       ","GOAT       ","LAUNDRY    ","BEE        ","TATTOO     ","RUSSIA     ","TIN        ","MAP        ","YELLOWSTONE","SILK       ","HOSE       ","SLOTH      "
defb "KUNG FU    ","CLOCK      ","BEAN       ","LIGHTNING  ","BOWL       ","GUITAR     ","RANCH      ","PEARL      ","FLAT       ","VIRUS      ","ICE AGE    ","COFFEE     ","MARATHON   ","ATTIC      "
defb "WEDDING    ","COLUMBUS   ","POP        ","SHERWOOD   ","TRICK      ","NYLON      ","LOCUST     ","PACIFIC    ","CUCKOO     ","TORNADO    ","MEMORY     ","JOCKEY     "
defb "MINOTAUR   ","BIG BANG   ","PAGE       ","SPHINX     ","CRUSADER   ","VOLCANO    ","RIFLE      ","BOIL       ","HAIR       ","BICYCLE    ","JUMPER     ","SMOOTHIE   "
defb "SLEEP      ","PENTAGON   ","GROOM      ","RIVER      ","FARM       ","JUDGE      ","VIKING     ","EASTER     ","MUD        ","PARROT     ","COMB       ","SALSA      "
defb "EDEN       ","ARMY       ","PADDLE     ","SALOON     ","MONA LISA  ","MILE       ","BLIZZARD   ","QUARTER    ","JEWELER    ","HAMBURGER  ","GLASSES    ","SAIL       "
defb "BOXER      ","RICE       ","MIRROR     ","INK        ","BEER       ","TIPI       ","MAKEUP     ","MICROWAVE  ","HERCULES   ","SIGN       ","PIZZA      ","WOOL       "
defb "HOMER      ","MINUTE     ","SWORD      ","SOUP       ","ALASKA     ","BABY       ","POTTER     ","SHOWER     ","BLADE      ","NOAH       ","SOAP       ","TUNNEL     "
defb "PEACH      ","DOLLAR     ","TIP        ","LOVE       ","JELLYFISH  ","STETHOSCOPE","TASTE      ","FUEL       ","MOSQUITO   ","WIZARD     ","BIG BEN    ","GARDEN     "
defb "WAITRESS   ","SHOOT      ","SHELL      ","LUMBERJACK ","MEDIC      ","DREAM      ","BLUES      ","EARTHQUAKE ","PEA        ","PARADE     ","SLED       ","SMELL      "
defb "COMPUTER   ","COW        ","PEANUT     ","WINDOW     ","MUSTARD    ","SAND       ","GOLF       ","CROW       ","ICELAND    ","APRON      ","VIOLET     ","DOOR       "
defb "TIGER      ","JOKER      ","HOUSE      ","COLLAR     ","HAWAII     ","DWARF      ","PINE       ","MAGICIAN   ","FROST      ","CURRY      ","BUBBLE     ","WOOD       "


		org 63800

UDGchars:								; UDGs
	defb 0,31,127,114,239,223,223,255
	defb 0,255,255,255,255,255,255,255
	defb 0,240,252,252,254,254,254,254
	defb 255,255,255,255,255,127,127,31
	defb 255,255,255,255,255,255,255,255
	defb 254,246,246,246,238,220,252,240
	defb 254,254,254,254,254,254,254,254




          chan_open	equ	0x1601
          make_room	equ	0x1655
          strms		equ	0x5c10
          chans		equ	0x5c4f
          prog		equ	0x5c53

         org 64000    ; FA00

;****************************************************************************
; CUSTOM 6x8 font - by me (Andy 'Uglifruit' Jenkinson)
;----------------------------------------------------------------------------
uglifont                      ; all now in the centre 6 pixels (768 BYTES)
    defb    0,0,0,0,0,0,0,0
    defb    24,24,24,24,0,24,24,0
    defb    20,20,0,0,0,0,0,0
    defb    20,62,20,20,62,20,0,0
    defb    28,42,40,28,10,42,28,0
    defb    50,50,4,8,22,22,32,0
    defb    24,32,26,36,36,36,26,0
    defb    8,8,0,0,0,0,0,0
    defb    12,16,16,16,16,16,12,0
    defb    24,4,4,4,4,4,24,0
    defb    8,42,28,8,20,34,0,0
    defb    0,8,8,62,8,8,0,0
    defb    0,0,0,0,24,8,16,0
    defb    0,0,0,60,0,0,0,0
    defb    0,0,0,0,0,24,24,0
    defb    4,4,8,8,8,16,16,0
    defb    28,34,34,34,34,34,28,0
    defb    8,24,40,8,8,8,62,0
    defb    28,34,2,4,8,18,62,0
    defb    28,34,2,12,2,34,28,0
    defb    4,12,20,36,62,4,30,0
    defb    62,32,60,2,2,34,28,0
    defb    28,34,32,60,34,34,28,0
    defb    62,2,4,30,8,8,28,0
    defb    28,34,28,34,34,34,28,0
    defb    28,34,34,30,2,34,28,0
    defb    0,24,24,0,24,24,0,0
    defb    0,24,24,0,24,8,16,0
    defb    4,8,16,32,16,8,4,0
    defb    0,0,62,0,0,62,0,0
    defb    16,8,4,2,4,8,16,0
    defb    28,34,4,8,0,24,24,0
    defb    28,38,42,44,32,34,28,0
    defb    8,20,20,34,62,34,34,0
    defb    60,34,34,60,34,34,60,0
    defb    28,34,32,32,32,34,28,0
    defb    60,34,34,34,34,34,60,0
    defb    62,32,32,60,32,32,62,0
    defb    62,32,32,56,32,32,32,0
    defb    28,34,32,38,34,38,26,0
    defb    34,34,34,62,34,34,34,0
    defb    28,8,8,8,8,8,28,0
    defb    30,4,4,4,36,36,24,0
    defb    34,36,56,36,34,34,34,0
    defb    32,32,32,32,32,32,62,0
    defb    34,54,54,42,42,34,34,0
    defb    34,50,42,42,38,34,34,0
    defb    28,34,34,34,34,34,28,0
    defb    60,34,34,60,32,32,32,0
    defb    28,34,34,34,42,36,26,0
    defb    60,34,34,60,36,34,34,0
    defb    28,34,32,28,2,34,28,0
    defb    62,8,8,8,8,8,8,0
    defb    34,34,34,34,34,34,28,0
    defb    34,34,34,20,20,8,8,0
    defb    34,34,42,42,20,20,20,0
    defb    34,34,20,8,20,34,34,0
    defb    34,34,20,20,8,8,8,0
    defb    62,2,4,8,16,32,62,0
    defb    28,16,16,16,16,16,28,0
    defb    16,16,8,8,8,4,4,0
    defb    28,4,4,4,4,4,28,0
    defb    8,20,34,0,0,0,0,0
    defb    0,0,0,0,0,0,0,126
    defb    12,18,16,60,16,26,54,0
    defb    0,0,30,34,34,38,26,0
    defb    32,32,60,34,34,34,60,0
    defb    0,0,28,34,32,34,28,0
    defb    2,2,30,34,34,34,30,0
    defb    0,0,28,34,62,32,28,0
    defb    0,12,16,60,16,16,16,0
    defb    0,0,30,34,30,2,28,0
    defb    32,32,60,34,34,34,34,0
    defb    0,8,0,24,8,8,28,0
    defb    0,4,0,4,4,4,24,0
    defb    32,32,36,56,36,34,34,0
    defb    0,24,8,8,8,8,28,0
    defb    0,0,60,42,42,42,42,0
    defb    0,0,44,50,34,34,34,0
    defb    0,0,28,34,34,34,28,0
    defb    0,0,60,34,60,32,32,0
    defb    0,0,28,36,28,6,4,0
    defb    0,0,44,50,32,32,32,0
    defb    0,0,28,32,28,2,60,0
    defb    0,8,8,28,8,8,4,0
    defb    0,0,34,34,34,38,26,0
    defb    0,0,34,34,20,20,8,0
    defb    0,0,34,42,42,42,20,0
    defb    0,0,34,20,8,20,34,0
    defb    0,0,34,34,30,2,28,0
    defb    0,0,62,4,8,16,62,0
    defb    12,8,8,16,8,8,12,0
    defb    8,8,8,8,8,8,8,0
    defb    24,8,8,4,8,8,24,0
    defb    0,0,18,44,0,0,0,0
    defb    60,66,90,82,90,66,60,0

      org 64768
      
      ; enter here to fill reset the attribute to 56 (white paper, black ink - poke-able at 64769,atrribute

      ld a,56
      ld (attribute),a

      ;enter here to clear the screen with the current sttribute    (64773   )

      ld hl,16384
      ld de,16385
      ld bc,6143
      ld (hl),0
      ldir

      ;enter here to flood fill the screen with just the current attribute (But not clear screen)  (64786  )

      ld a,(attribute)
      ld hl,23295
      ld de,23294
      ld (hl),a
      ld bc,767
      lddr

      ;enter here to just reset the print position, etc (except attribute), to 0,0,over off,no columns,inverse off    (64801   )

      ld hl,resetmessageaddress
      ld bc,endofresetmessage-resetmessageaddress
loopprintmessage
      ld a,(hl)
      call rst16replacement
      inc hl
      dec bc
      ld a,b
      or c
      jr nz,loopprintmessage
      EI
      ret

      ;enter here for custom message printing                      64819

      ;ld hl,startcustommessageaddress                     ; POKABLE     64820/21
      ;ld bc,endofcustommessage-startcustommessageaddress  ; POKABLE     64823/24
      LD HL,$1539              ; (c) Sinclair Research message
      ld bc,25

      jr loopprintmessage

resetmessageaddress
       defb 22,0,0      ; at 0,0
startcustommessageaddress
       defb 21,0        ; over off
       defb 20,0        ; inverse off
       defb 19,0,18,42  ; column limits
       defb 17,255      ; PAPER mask
endofresetmessage equ $
endofcustommessage equ $



      org 64840
                              ;enter at 64840 to copy from system colours to print routine colours
         ld de,(23693)
         ld (attribute),de
         ret
         org 64849            ;enter at 64840 to copy from 42 column attributes to system colours
         ld de,(attribute)
         ld (23693),de
         ret


          org 64858                  ; this routine has been hacked in, solely to stop printing outside of screen
printacharactercheckcoordsarevalid
          ld a, (ycoord)
          cp 24
          jr c, yok
          ld a,23
          ld (ycoord),a
yok
          ld a, (xcoord)
          cp 42
          jr c, xok
          ld a,41
          ld (xcoord),a
xok
          jp printacharacter




          org 64885
;****************************************************************************
; POKEable flags, 14 bytes
;----------------------------------------------------------------------------
fontaddress  defw uglifont
attribute    defb 76     ; ATTRIBUTE to be printed
attributemask defb 255      ; mask for colours
newattribute defb 49      ; temporarily used in 'overprinting' colours
overflag     defb 0      ; OVER flag
inverseflag  defb 0      ; INVERSE flag - if bit 1 is set, the routine inverts for the printed character
tabposition  defb 0      ; TAB position that a carriage return puts you to; default = 0 (flush left)
maxcolumn    defb 41     ; Maximum width of screen, used for coulmns
xcoord       defb 0      ; co-ordinates for printing routine x=0 to 40
ycoord       defb 0      ; co-ordinates for printing routine y=0 to 21
actualxcoord defb 0      ; used to store actual coordinate (of 32 columns)
ycoordtemp   defw 0      ; used in columnshift

;-------------------------------------------------------------------------------
;SETUP channel 7 ('T') for priting from basic, etc
;58 bytes
;---------------------------------------------------------------------------------
         org 64900
setupchannels
         di            ; This routine ...  http://shadowmagic.org.uk/cssfaq/reference/48kreference.htm thanks to Andrew Owens
         LD HL,(prog)  ; A new channel starts below PROG
         DEC HL        ;
         LD BC,0x0005  ; Make space
         CALL make_room   ;
         INC HL        ; HL points to 1st byte of new channel data
         LD A,$C1     ; LSB of output routine                    <----------------------- address of routine
         LD (HL),A     ;
         INC HL        ;
         PUSH HL       ; Save address of 2nd byte of new channel data
         LD A,$FD     ; MSB of output routine
         LD (HL),A     ;                                       <----------------------- address of routine
         INC HL        ;
         LD A,0xc4     ; LSB of input routine
         LD (HL),A     ;
         INC HL        ;
         LD A,0x15     ; MSB of input routine
         LD (HL),A     ;
         INC HL        ;
         LD A,"T"      ; Channel name 'T'
         LD (HL),A     ;
         POP HL        ; Get address of 2nd byte of output routine
         LD DE,(chans) ; Calculate the offset to the channel data
         AND A         ; and store it in DE
         SBC HL,DE     ;
         EX DE,HL      ;
         LD HL,strms   ;
         LD A,7        ; Stream to open, in this case 7.
         ADD A,0x03    ; Calculate the offset and store it in HL
         ADD A,A       ;
         LD B,0x00     ;
         LD C,A        ;
         ADD HL,BC     ;
         LD (HL),E     ; LSB of 2nd byte of new channel data
         INC HL        ;
         LD (HL),D     ; MSB of 2nd byte of new channel data
         ei
         ret



;*********************************************************************************
; the RST $10 replacement routine
; accepts: CHR$22,Yccord,Xccord
; CHR$13, 
; AT,OVER,INVERSE,INK,PAPER,FLASH,BRIGHT   < note different use of Ink, Paper, Bright and Flash
; Routine 573 bytes
;---------------------------------------------------------------------------------

             ORG 64961     ;       $FDC1
rst16replacement         ; the printroutine to replace RST 10/be used in basic
             di
             push hl
             push de
             push bc
             push af
             push af          ; temporarily save the called character


            ; call flags      ;*************************************************************************testing


             ld a,(atflag)    ; are we mid way through an 'AT command' or similar?
             and a
             jp z, notatting  ; if it is zero we can carry on
             ld b,a              ; counter for which co-ordinate also stores 'a' temporarily

                and %11111100  ; test flag for other control control codes, except for 'at'
                jr z, itsanat  ; if none of the others are set, it must be an 'at' command
                               ; bits 0-7 indicate control code type
                               ; xxxxxx01,xxxxxx10 = AT x/y coordinate follows
                               ; xxxxx1xx = OVER command data follows
                               ; xxxx1xxx = INVERSE command data follows (inverse >1 will also be used for special printing techniques)
                               ; xxx1xxxx = FLASH command data follows
                               ; xx1xxxxx = BRIGHT command data follows
                               ; x1xxxxxx = PAPER command data follows
                               ; 1xxxxxxx = INK command data follows
                pop af         ; get the data element into A register

                rrc b
                rrc b

                rrc b        ; is it OVER data
                jr nc, notover
                ld hl,overflag    ; load the OVER flag with that data
                ld (hl),a
                jr clearatflag
notover
                rrc b        ; is it INVERSE data
                jr nc, notinverse
                ld (inverseflag),a           ; load the INVERESE flag with that data, regardless of value
                jr clearatflag
notinverse
                rrc b            ; is it flash data
                jr nc, notflash
                ld (tabposition),a  ; test  FLASH sets tab position
                jr clearatflag
notflash
                rrc b             ; is it bright data
                jr nc, notbright
                ld (maxcolumn),a  ; test  BRIGHT sets max column width
                jr clearatflag
notbright
                rrc b              ; is is PAPER data?
                jr nc, notpaper
                ld (attributemask),a  ; use PAPER as atrribute mask
                jr clearatflag
notpaper
                ld (attribute),a
                jr clearatflag

clearatflag
           xor a
           ld (atflag),a        ; clear the atflag, as we have dealt with the ctrlcode
           jr dontprint

itsanat      ld a, b
             dec a
             ld (atflag),a
             ld hl,xcoord-1    ; need to load the coordinates with the values
atflagloop   inc hl
             djnz atflagloop
             pop af            ; restore the called character (which is a coordinate)
             ld (hl),a         ; store the coordinate
             jr dontprint      ; skip calling the print routine
notatting
        pop af            ; restore the called character
        cp " "            ; is it printable
        jr nc, printable

        cp 13             ; is it a carriage return chr$13
        jr nz, notacr

        ld a,(tabposition)       ; this will change for tabbed printing
        ld (xcoord),a
        ld a,(ycoord)
        inc a
        ld (ycoord),a
        jr dontprint       ; skip calling the print routine
notacr
        cp 22              ; is it an 'AT' ctrl chr$
        jr nz, notatchr
        ld a,2             ; set the atflag for coordinates later
        ld (atflag),a
        jr dontprint       ; skip calling the print routine

notatchr
        sub 15 ; take off 15, so now control codes for ink=1,paper=2,flash=3,bright=4,inverse=5,over=6
        ld b,a

        ld a,%00000001
slideinflag
        rrca
        djnz slideinflag

        ;ld a,4

        ld (atflag),a
        jr dontprint       ; skip calling the print routine

printable
        ld (characterno),a
        call printacharactercheckcoordsarevalid
        call attrbdo
        
dontprint
        pop af
        pop bc
        pop de
        pop hl
        ei
        ret


printacharacter
        call getchradd
        call copytoscratch
        call populatecoords
        call shiftscratch
        ld a,(actualxcoord)
        ld e,a
        ld d,0
        ld hl,16384
        add hl,de
        ld a,(ycoord)
        push af
        and %11000
        or h
        ld h,a
        pop af
        and %111
        rrca
        rrca
        rrca
        or l
        ld l,a

        ld a,(overflag)
        and 1                  ; is bit one set? if yes, then this is OVERPRITING!
        jr nz,skipoverprint
        push hl                  ;tis is ignored if 'over' printing flag is set.
        call filloversctrach     ;
        call overshiftscratch    ;
        pop hl                   ;
        push hl                  ;
        call overscratchtoscreen ;
        pop hl                   ;
skipoverprint

        call scratchtoscreen
 
        ld de,(ycoord)
        ld (ycoordtemp),de

        ld a,(maxcolumn)
        ld b,a
        ld a,(xcoord)
        inc a
        ld (xcoord),a
        cp b
        ret nz

        ld a,(tabposition)
        ld (xcoord),a
        ld hl,ycoord
        inc (hl)
        ret



getchradd       ld a,(characterno) ; getscharater from charterno variable
                sub 32
                ld e,a
                ld d,0
                rl e ; multiply by 8
                rl d
                rl e
                rl d
                rl e
                rl d
                ld hl,(fontaddress)
                add hl,de
                ret

shiftscratch
                ld a,(shiftamount)
                or a
                ret z
                ld b,a
nextshift       push bc
                ld b,8
                ld hl,scratchspace
nextshiftline   or a
                rr (hl)
                inc hl
                rr (hl)
                inc hl
                djnz nextshiftline
                pop bc
                djnz nextshift
                ret


copytoscratch   ;copy to scratchspace called with hl pointing at character.
                ld de,scratchspace
                ld b,8
                ld c,0       ; used for inverse
                ld a,(inverseflag) ;does it need inverting
                rrca
                jr nc,nexttoscr              ; if not, move along
                ld c,255
nexttoscr       ld a,(hl)
                xor c              ; invert it if need be - c=255 or c=0 if inverse set/unset
                and %01111110      ; just in case font uses outsides
                rlca               ; shifts it into leftmost position
                ld (de),a
                xor a
                inc de
                ld (de),a
                inc de
                inc hl
                djnz nexttoscr
                ret

scratchtoscreen     ; called with hl pointing at the screen address
               ld de,scratchspace
               ld b,8
scrscrloop     
               ld a,(de)
               or (hl)
               ld (hl),a
               inc hl
               inc de
               ld a,(de)
               or (hl)
               ld (hl),a
               inc de
               dec hl
               inc h
               djnz scrscrloop
               ret




filloversctrach                ;fills the overscratch space with %00000011 %11111111 to shift, then AND with screen contents
               ld b,8
               ld hl,overscratch
overpopulate   ld (hl),%00000011
               inc hl
               ld (hl),255
               inc hl
               djnz overpopulate
               ret

overshiftscratch                ;shifts the oversctrachspace as many times as needed
                ld a,(shiftamount)
                or a
                ret z
                ld b,a
nextovershift   push bc
                ld b,8
                ld hl,overscratch
nextovershiftline
                scf
                rr (hl)
                inc hl
                rr (hl)
                inc hl
                djnz nextovershiftline
                pop bc
                djnz nextovershift
                ret

overscratchtoscreen     ; called with hl pointing at the screen address
               ld de,overscratch
               ld b,8
overscrscrloop
               ld a,(de)
               and (hl)
               ld (hl),a
               inc hl
               inc de
               ld a,(de)
               and (hl)
               ld (hl),a
               inc de
               dec hl
               inc h
               djnz overscrscrloop
               ret



populatecoords            ; sets shift amount, and actual coordinate for x position
               ld a,(xcoord)
               ld d,0
               ld e,a
               ld hl,coordslookup
               add hl,de
               ld a,(hl)
               push af
               and %00111111
               ld (actualxcoord),a
               pop af
               and %11000000
               rlca
               rlca
               rlca
               ld (shiftamount),a
               ret


characterno  defb 0      ; the character that is being printed
atflag       defb 0      ; used during printing of ctrl chr$
shiftamount  defb 0      ; used in shifting

coordslookup       ; lookuptable for printing x position. LSB xx111111 = character position MSB 11xxxxxx = shift-to-right/2 00=0 01=2 10=4 11=6
             defb 0,192,129,66,3,195,132,69,6,198,135,72,9,201,138,75,12,204,141,78,15,207,144,81
             defb 18,210,147,84,21,213,150,87,24,216,153,90,27,219,156,93,30,222,159
scratchspace      ; used to shift character before printing to screen
             defb 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0                        ; 16 bytes
overscratch      ; used to clear space before printing to screen
             defb 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0                        ; 16 bytes, only used when 'over' is set

;---------------------------------------------
;SETS THE ATTRIBUTES ....
;61 BYTES LONG
;---------------------------------------------
attrbdo

        ld a,(attributemask)
        cp 255 ; if we're not changing FLASH,BRIGHT,PAPER,INK then just return, having done nothing
        ret z

            ld a,(attributemask)           ; use the attribte mask
            xor 255                        ; inverted
            ld b,a                         ; in order to see which
            ld a,(attribute)               ; colours are to be
            and b                          ; replaced
            ld (newattribute),a            ; store these in 'new attribte'

        ld de,(ycoordtemp)                              ;calculate from X,Y coords D=X E=Y, giving HL=attrb address
        ld h,0
        ld l,e
        add hl,hl ;makes 16-bit shift to the left
        add hl,hl
        add hl,hl
        add hl,hl
        add hl,hl
        ld e,d
        ld d,0
        add hl,de
        ld a,h ;adding to the MSB is faster than ld de,32:add hl
        add a,#58
        ld h,a

        ld a,(attributemask)       ; use attribte mask
        ld b,a                     ; to decide which attribtes to keep
        ld a,(hl)                  ; then
        and b                      ; OR it with the incoming colour
        ld b,a                     ; attribute
        ld a,(newattribute)        ; and write it
        or b                       ; to the screen

        ld(hl),a
        ld a,(shiftamount)
        and %100              ;is it 4 or over
        ret z                 ; if not, just that attribute is fine, and we can return
        ld a,(hl)
        inc hl                ; otherwise we need to fill the next coordinate too
        ld(hl),a
        ret




        end  50000  ;setupchannels



