R800 Programming
This page was last modified 01:05, 5 October 2017 by Gdx. Based on work by Sd snatcher and Mars2000you and others.

The R800 is the CPU of the MSX Turbo R.

Contents

How to change the CPU mode in assembly

BIOS routines

In MSX tR there are two CPU related BIOS-calls:

CHGCPU (#180) A-register contains the CPU-mode:

  • 0 = Z80
  • 1 = R800 (ROM)
  • 2 = R800 (DRAM)

If bit 7 is set also CPU LED will be updated.

GETCPU (#183) A-register returns CPU-mode in same format as with CHGCPU. bits 2-7 are always "0".

Tip: to enable the turbo by software, it's best to use the following routine:

CHGCPU	equ	0180h
 
ENATURBO:
	ld	A,(CHGCPU)
	cp	0C3h
	ld	a,82h              ; can be ld a,81h for R800 ROM if you wish
	call	z,CHGCPU
	;

This routine will enable the turbo on any machine that has the CHGCPU, without restricting it to MSX Turbo-R models. There are some turbo Z80 kits that implement the CHGCPU/GETCPU routines, but many will not have the DRAM mode off course. Their GETCPU always return ROM-mode when the turbo is enabled.

Deeper knowledge

The whole CPU control is handled by only two bits on the S1990. S1990 works so that you write register number to I/O port #E4 and then you read/write data using I/O port #E5. CPU mode handling is located in register 6.

Here is the meaning of the bits of register 6:

  • bit 5: Processor mode (0=R800, 1=Z80)
  • bit 6: ROM mode (0=DRAM, 1=ROM)

When you access Processor mode bit directly, the most important thing is that you keep track where both of the CPU's PC-registers point to. Also when you change from Z80 to R800 there seems to be a trick that should be used: in case of Z80 the code in BIOS uses OTIR to move wanted CPU mode to E5, but it also moves Z80 ROM mode request inside same instruction (2 byte move).

I think the reason for this might be that when R800 switches back to Z80 it does not leave busses fast enough and this trick is used to prevent conflict situation on data- and address bus. (OTIR it self makes sure that PC does not get increased too early and Z80 ROM was same request that was made by R800 anyway) This is however pure speculation.

If you want to know more about CPU change, here is how the CPU initialization & change happens in MSX tR BIOS:

J0000:	DI				; Both Z80 and R800 will start execution from here (address 0)
	JP	J126B
 
;----------------------------------------
 
J126B:	NOP
	IN	A,(0F4H)
	BIT	7,A			; ´cold´ boot ?
	JR	Z,J127A			; yep,
	LD	A,80H
	CALL	C046A			; CHGCPU: Z80 mode, led changed
	DI
	JR	J1292			; start init
 
J127A:	BIT	5,A			; R800 initialized ?
	JR	NZ,J1295		; yep, continue
	OR	20H
	OUT	(0F4H),A
 
	LD	A,02H			; (A1ST has also PCM init here)
	OUT	(0A7H),A		; pause led off, turbo led off, b1=1
 
	LD	A,6
	OUT	(0E4H),A
	LD	HL,I04E5
	LD	BC,02E5H
	OTIR				; R800 ROM mode, Z80 waits here for the R800 to complete
J1292:	JP	J0416			; orginal init like in other MSX-computers
 
J1295:	IM	1
	XOR	A			; Z80 mode, led unchanged
	JP	J0489			; jump in CHGCPU to halt R800 and resume the Z80
 
 
----------------------------------------
;	Subroutine	CHGCPU
;	Inputs		A(b6-b0) = cpumode, A(b7) = update turboled
;	Outputs		________________________
 
; JP on #180 points to this address
 
C046A:	PUSH	AF
	AND	7FH
	CP	03H
	JR	NC,J0468		; invalid cpumode, quit
	POP	AF
	DI
	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	AF
	PUSH	IX
	PUSH	IY
	EXX
	EX	AF,AF'
	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	AF			; save all Z80 registers
	LD	A,I
	PUSH	AF			; save interrupttable pointer
	LD	(D.FFFD),SP		; save stackpointer
	EX	AF,AF'
J0489:	AND	83H
	LD	C,A
	LD	B,00H
	LD	HL,CASPRV
	LD	A,(HL)
	SLA	C
	RES	1,A
	JR	NZ,J049A		; R800, b1=0
	SET	1,A			; Z80, b1=1
J049A:	JR	NC,J04A2		; flag update turboled not set, turboled unchanged
	RES	7,A
	JR	Z,J04A2			; Z80, turboled off
	SET	7,A			; R800, turboled on
J04A2:	LD	(HL),A
	OUT	(0A7H),A
	LD	HL,I04E3
	ADD	HL,BC
	LD	A,6
	OUT	(0E4H),A
	IN	A,(0E5H)
	INC	B
	BIT	5,A
	JR	Z,J04B5
	INC	B
J04B5:	LD	C,0E5H
	OTIR
	DI
	NOP
	LD	SP,(D.FFFD)		; restore stackpointer
	POP	AF
	LD	I,A			; restore interrupttable pointer
	POP	AF
	POP	BC
	POP	DE
	POP	HL
	EXX
	EX	AF,AF'
	POP	IY
	POP	IX
	POP	AF
	POP	BC
	POP	DE
	POP	HL			; restore all Z80 registers
	EI
	RET
;----------------------------------------
 
I04E3:	DEFB	060H,060H		; Z80
I04E5:	DEFB	040H,060H		; R800 ROM
	DEFB	000H,060H		; R800 RAM

Differences of R800 and Z80

Although most software works ok on R800 this is not 100% compatible with Z80

New instructions

R800 has some new instructions. MULUB do 8bit multiplication unsigned and MULUW instructions do 16bit multiplication unsigned. Please note that instructions that are marked with RED are not guaranteed to work properly.

Opcode MNEMONIC
ED F9 MULUB A,A
ED C1 MULUB A,B
ED C9 MULUB A,C
ED D1 MULUB A,D
ED D9 MULUB A,E
ED E1 MULUB A,H
ED E9 MULUB A,L
ED C3 MULUW HL,BC
ED D3 MULUW HL,DE
ED E3 MULUW HL,HL
ED F3 MULUW HL,SP

In MULUB instructions the result is always stored to HL and in MULUW instructions the result is always stored to DE:HL

Execution time:

MULUB takes 1.96 μsec
MULUW takes 5.03 μsec

Flags state after execution:

S <- 0
Z is set when result is zero
H does not change
P/V <- 0
N does not change
C is set when result doesn't fit in 8-bit (MULUB) or 16-bit (MULUW)

Changed instructions

Things that may cause compatibility problems with Z80 software:

  • Undocumented Z80-operation "SLL" have been replaced with another undocumented instruction similar to "TEST" of the Z280
  • Undocumented flags represented in bits 3 and 5 of the F register don't assume the same values as in Z80
  • Undocumented instruction OUT (C),0

Several other undocumented instructions have changed. (See this opcodes list for more details.)

Trivia about the MSX turbo-R CPU-modes

There seems to be lot of confusion of how MSX tR CPU modes work. This document tries to help preventing most common misunderstandings.

LED does not indicate wich CPU is in use

The CPU LED has nothing to do with the CPU mode itself. This is a software controlled LED that is connected to I/O port #A7 bit 7.

R800 mode <> MSX-DOS2

There's no link between operating system and CPU mode. It is true that the MSX turbo-R selects R800 as default CPU when MSX-DOS2 is started, but MSX-DOS2 works just as well with Z80. Although for example MSX-DOS1 boots with Z80, it can be used with R800 just as well. These are just software defaults that can be changed.

Old software can be run on R800

R800 and Z80 are very similar CPU's. Although R800 and Z80 have some minor differences, most Z80 software can run also on R800 without CPU related problems. R800 is however a much faster CPU and that may cause software to look or sound different. The problems are usually the same as with other MSX 7MHz upgrade kits that hobbyists have made. Most often the CPU itself is rarely the cause of problem.

"Z80, R800, R800 DRAM" doesn't automatically mean "Slow, Faster, Fastest"

Although speed increases like this in many cases there are also exceptions. R800 DRAM mode is faster than R800 mode only if the application uses BIOS calls. If you use, for example, SymbOS then you will not gain any speed but just loose 64KB of RAM. Also if the application is extremely VDP intensive the R800 may actually run slower than Z80. The EVA-player is good example of this.

DRAM mode can be used also on MSX-DOS1 and other older applications. DOS2 is not required

It is true that DRAM mode uses 4 last pages of memory mapper to store a copy of ROM, and therefore applications can't use this area when DRAM mode is in use.

However, DRAM mode was designed so that it looks like there is no RAM at all on these pages. This means that writing to these addresses don't have any effect and if you try to read this memory area you get only #FF as return. This means that usually programs just detect the mapper smaller than it actually is and don't even try to use this area. So, usually older applications work as well.

On MSX turbo-R, MSX-DOS2 always reserves this space for DRAM mode by default and if you want to use it for other purposes, you need to request DOS2 to free it. (See DOS2 mapper support routines)

S1990 does not copy the BIOS to 4 last pages of mapper

The copying is done by CPU during boot. The routine can be found from SUB-ROM address #3F1C. S1990 can not execute any code or transfer data on memory.

Z80 DRAM mode is not a rumour

This mode is not supported by the BIOS routine, but it can be accessed by using hardware directly. The most easy way to access it is to execute:

 
	XOR A
	CALL #180
	LD A,#20
	OUT (#E5),A

This will not increase the speed, but it can be used to enable running modified BIOS. Modifications need to be done in some ROM-modes because otherwise the memory is not available.

R800 and Z80 do not share any registers

When the CPU is switched, the CPU simply stops and keeps all the registers unchanged. The BIOS routine takes care that registers are copied from one CPU to another. In order to do that both CPU's must be guided to the same routine as they both start execution from address 0 when they are powered on for first time.

When the turbo-R boots, S1990 will automatically select the Z80 ROM CPU mode. I/O port #F4 is used to keep track if the R800 CPU has been initialized or not.

Example programs to change CPU

Here are some neat "trick"-programs that can be used to change CPU

Assembler

 
; On MSX tR changeing CPU expetially on BASIC is usually hard. Either
; you have to write complex BASIC programs or you need to load some
; sort of BASIC extension to get needed functionality. Now there is
; finally one command solution called EasyCPU!
;
; Here is how you can use this program...
; 
; BASIC:
;
; BLOAD"EASYCPU.COM",R,<CPUMODE>
;
; DOS:
;
; EASYCPU <CPUMODE>
;
; <CPUMODE>:
; 0 = Z80
; 1 = R800 ROM
; 2 = R800 DRAM
;
; ~NYYRIKKI
;
 
        OUTPUT EASYCPU.COM
 
	CP 0
	ADD A,THEEND
	ADD A,1
	ADD A,0
 
	ORG 1
 
	LD HL,#2D
	LD A,#80
	CALL #C
	EX AF,AF'
	DEC SP
	LD DE,#309
	DEC SP
	POP HL
	LD A,L
	SUB E
	LD C,A
	LD B,H
	DEC H
	JR NZ,SKIP
	LD A,(#82)
	XOR #30
SKIP:
	EX AF,AF'
	CP D
	JR NZ,NOTR
	EX AF,AF'
	CP D
	JR NC,ICPU
	OR #80
	DW #80F7,384
	RET
NOTR:
	LD HL,NOTRTXT
	JR PRINT
ICPU:
	LD HL,ICPUTXT
PRINT:
	ADD HL,BC
	EX DE,HL
	LD C,L
	JP #F37D
 
NOTRTXT:
	DEFB "This program is for MSX tR only.",#D,#A,"$"
ICPUTXT:
	DEFB "Illegal CPU mode.",#D,#A,"$"
THEEND:

BASIC

 
8728 DEFUSR=39+PEEK(-2385)+PEEK(-2384)*256:IFUSR(0)THENLINE>1COPY0&H80F6OR384ALLYOURBASEAREBELONGTOUS!

Change number after "LINE>" like this: 0 = Z80 (ROM) 1 = R800 (ROM) 2 = R800 (DRAM)

You can use this in your BASIC programs or if you type only this one line and save it with .COM extension you can use this program also in DOS. Example: SAVE"Z80.COM" _SYSTEM Z80

~NYYRIKKI