Accessing output pins in joystick ports

By nicalejo

Supporter (10)

nicalejo's picture

11-04-2012, 21:27

Initially posted in the spanish forum.

Hi all, I am new here because I recently purchased a toshiba hx-10. I always wanted to have a msx and now I feel great. I decided to start programming some basic code and I need to send some kind of signal outside the msx. I read in specs that 6, 7 and 8 pins in both joysticks are INPUT/OUTPUT. It is easy to read the input values but ¿does anyone how to write those pins to get +5v outside?

I would like to connect an arduino device. If not possible ¿any example of code to write in expansion bus?

Thaks in advance

Login or register to post comments

By caro

Hero (513)

caro's picture

12-04-2012, 07:39

;****************************************
; SPI for MSX *
;****************************************
; MISO ->: Up (Joy-1pin) PA/0
; VDD <-: +5V (Joy-5pin)
; MOSI <-: TrgA (Joy-6pin) PB/0 | PB/2
; SCK <-: TrgB (Joy-7pin) PB/1 | PB/3
; RESET <-: STROBE (Joy-8pin) PB/4 | PB/5
; GND : GND (Joy-9pin)
;----------
; SEL 1|2 : PB/6=0|PB/6=1
;----------------------------------------
PUBLIC set_j1,set_j2,res_joy
PUBLIC imp_SCK,clr_SCK
PUBLIC pas_RES,act_RES
PUBLIC rdser,wrser
.z80
;****************************************
PSG_A equ 0a0h ;reg address
PSG_W equ 0a1h ;reg data WR
PSG_R equ 0a2h ;reg data RD
;========================================
; Set port Joy 1
set_j1: ld a,15 ;address PORT B
out (PSG_A),a
in a,(PSG_R) ;RD port B
ld (JOY_S0),a ; save
and 08fh ;SEL=0 (PB/6=0)
;RESET=0 (PB/4,5=0)
or 00fh ;MOSI=1,SCK=1
jr ini_set
;========================================
; Set port Joy 2
set_j2: ld a,15 ;address PORT B
out (PSG_A),a
in a,(PSG_R)
ld (JOY_S0),a
and 0cfh ;RESET=0 (PB/4,5=0)
or 04fh ;SEL=1,MOSI=1,SCK=1
ini_setShocked!ut (PSG_W),a ;write in B
ld (JOY_S1),a ; save code
ret
;------------------------------------------------
; restore ports
res_joy:ld a,15 ;порт B
out (PSG_A),a ;
ld a,(JOY_S0)
out (PSG_W),a
ret
;--------------------------------------------------
JOY_S0: db 0
JOY_S1: db 0
;==================================================
;*** SCK = 0 (PB/3,1=0)
clr_sck:ld a,15 ;port B
out (PSG_A),a
ld a,(JOY_S1)
jr ini2
;--------------------------------------------------
; SCK _/~\_ (PB/3,1)
imp_sck:ld a,15 ;port B
out (PSG_A),a
ld a,(JOY_S1)
or 08h OR 02h ;SCK = 1 (TRGB2=H)
; ; (TRGB1=H)
out (PSG_W),a
ini2: and 0f7h AND 0fdh ;SCK = 0 (TRGB2=L)
; ; (TRGB1=L)
out (PSG_W),a
or 04h OR 01h ;MOSI =1 (TRGA2=H)
; (TRGA1=H)
ld (JOY_S1),a
ret
;--------------------------------------------------
;*** RESET control (PB/4,5)
pas_res:ld a,15 ;port B
out (PSG_A),a
ld a,(JOY_S1)
or 30h ;RESET=1 (STB=1)
jr pa_res
;--------------------------------------------------
act_res:ld a,15 ;port B
out (PSG_A),a
ld a,(JOY_S1)
and 0cfh ;RESET=0(STB=0)
pa_res: out (PSG_W),a
ld (JOY_S1),a
ret
;==================================================
; receive byte [result -> (C)]
rdser: ld c,0 ;send byte = 0
; send byte from (C)
wrser: ld b,8 ;counter bits
ld a,15 ;port B
out (PSG_A),a
;/---- Cycle of reception and transfer of byte ----\
wrckl:
;----
ld a,(JOY_S1) ;MOSI = H
rl c ;CY <- data_out(7)
jr c,wr_cy
and 0fbh AND 0feh ;MOSI = L (TRGA2=L)
; (TRGA1=L)
wr_cy: out (PSG_W),a ;
ld (JOY_S1),a
;----
ld a,14 ;port A
out (PSG_A),a
in a,(PSG_R) ;RD port A
push af
call imp_SCK ; SCK _/~\_
pop af
rra ;data_in -> CY
djnz wrckl ; To repeat 8 time
;
rl c ; (C) result
ret
;=================================
end

By nicalejo

Supporter (10)

nicalejo's picture

12-04-2012, 07:56

Thanks a lot. It's amazing to see people ready to help so fast.

By nicalejo

Supporter (10)

nicalejo's picture

12-04-2012, 10:20

I think I am not yet ready for assembler but I will try to write those pins in basic using the ports. One more question (maybe dumb): once I set to High a pin ¿how long does it stay in this status?

By caro

Hero (513)

caro's picture

12-04-2012, 13:45

To guarantee a stable condition of lines of port it is not possible, as BASIC regularly interrogates these ports at occurrence of interruptions from the Video-controller.
Those procedures, which I have laid out above, can normally function only at the disable interruptions.

By nicalejo

Supporter (10)

nicalejo's picture

12-04-2012, 15:07

Thanks again. As mentioned above I want to use it with an arduino board. I have realized that there is a library for arduino that implements SPI protocol. http://arduino.cc/en/Reference/SPI
Wiring schema

I think arduino can be perfectly wired with the pin reference on top of your code and should work. If you can give me your opinion would be great.

So as a newbie I should generate a binary from your assembler code, load it into memory and call the routines from basic. Let's imagine I am able to reach this point :) : Could you give a basic example of how to call and use this library to send and receive data?

Thanks caro

By Gradius2

Hero (657)

Gradius2's picture

12-04-2012, 18:25

Please, ALWAYS use [ code ] option for those codes!

By nicalejo

Supporter (10)

nicalejo's picture

13-04-2012, 07:52

OK, now I know how to call assembly code from basic, it is fairly easy. So I just need to know the purpose of the PUBLIC routines:

PUBLIC set_j1,set_j2: Seems evident, wich joy we want to use for the transfer
PUBLIC res_joy: ¿Reset joystick? OK. When?
PUBLIC imp_SCK,clr_SCK: ¿?
PUBLIC pas_RES,act_RES: ¿Passive , Active?
PUBLIC rdser,wrser: Reas, write byte

How do I use imp_SCK,clr_SCK?

By faxerpelle

Expert (98)

faxerpelle's picture

09-05-2012, 11:01

Trying to approach this solution too.

Just for clarification since I'm not so deep into ASM too, there is an instruction in the above code which starts with ini_set and somehow, don't know if it's my browser, there's a smiley just above the code line.

What's the correct instruction then? ini_setut or ini_set ut or ini_set out (only examples)?

I haven't found any clarification googling it and again i'm not so deep in ASM yet.

Ciao!