Jump to content
IGNORED

SaveKey programming help


kenfused

Recommended Posts

I am trying to get savekey working on the 7800. The problem I am having is even when the SaveKey is not plugged in, it doesn't produce an error when I read from it. Is there any way I can detect if one is plugged in? Is the problem when it is not plugged in the value will still produce the same value as when the code reads back the status bit?

 

Thanks,

Ken

 

SWCHA		   =	 $280   ;P0, P1 Joystick Directional Input
CTLSWA		  =	 $281   ;I/O Control for SCHWA
I2C_SDA_MASK	=	 $04
I2C_SCL_MASK	=	 $08



.importzp memCardZpTempWord   ; only used (temporarily) if MemCardWrite and MemCardRead functions are used
.import   memCardTemp		  ; temp used for almost all functions
.export MemCardReadPage, MemCardWritePage

;a/x points to buffer
;y is byte count
MemCardWritePage:
	   sta memCardZpTempWord
	   stx memCardZpTempWord+1
		   sty memCardTemp
		   lda #0
		   lsr memCardTemp
		   ror a
		   lsr memCardTemp
		   ror a
		   pha
		   lda memCardTemp
		   pha
		   jsr i2c_startwrite
		   bcs @error
		   pla
		   jsr i2c_txbyte
		   pla
		   jsr i2c_txbyte
		   ldy #0
@lpmcwr:	   lda (memCardZpTempWord),y
		   jsr i2c_txbyte
		   iny
		   cpy #64
		   bne @lpmcwr
		   jsr i2c_stopwrite
		   clc
		   rts
@error:		pla
		   pla
		   sec
		   rts

	
;a/x points to buffer
;y is page number  (64 byte pages)
MemCardReadPage:
	   sta memCardZpTempWord
	   stx memCardZpTempWord+1
		   sty memCardTemp
		   lda #0
		   lsr memCardTemp
		   ror a
		   lsr memCardTemp
		   ror a
		   pha
		   lda memCardTemp
		   pha
		   jsr i2c_startwrite
		   bcs @error
		   pla
		   jsr i2c_txbyte
		   pla
		   jsr i2c_txbyte
	   jsr i2c_stopwrite
	   jsr i2c_startread

		   ldy #0
@lpmcrd:	   jsr i2c_rxbyte
		   sta (memCardZpTempWord),y
		   iny
		   cpy #64
		   bne @lpmcrd
		   jsr i2c_stopread
		   clc
		   rts
@error:		pla
		   pla
		   sec
		   rts



i2c_start:
	lda	 #I2C_SCL_MASK
	sta	 SWCHA
	lda	 #I2C_SCL_MASK|I2C_SDA_MASK
	sta	 CTLSWA
rts

i2c_stop:
	lda	 #$00
	sta	 SWCHA
	lda	 #I2C_SCL_MASK|I2C_SDA_MASK
	sta	 CTLSWA
	lda	 #I2C_SCL_MASK
	sta	 SWCHA
	lda	 #$00
	sta	 CTLSWA
rts

;--------------------------------------------------------------------
;i2c+txbit - transmit a bit.
;
; parameters:
;	carry flag = bit to transmit
;
; returns:
;	nothing
;
; notes:
;	x and y are preserved
;--------------------------------------------------------------------
i2c_txbit:
	lda	 #$00
	sta	 SWCHA
	lda	 #$01
	rol a		;shift in carry
	asl a
	asl a
	sta	 CTLSWA ; SDA = !C
	lda	 #I2C_SCL_MASK
	sta	 SWCHA
rts

;--------------------------------------------------------------------
;i2c+txack - transmit an ack
;i2c+txnak - transmit an nak
;
; parameters:
;	none
;
; returns:
;	nothing
;
; notes:
;	x and y are preserved
;--------------------------------------------------------------------
i2c_txack:
	lda	 #$00
	sta	 SWCHA
	lda	 #I2C_SCL_MASK|I2C_SDA_MASK
	bne	 com1
i2c_txnak:
	lda	 #$00
	sta	 SWCHA
	lda	 #I2C_SCL_MASK
com1:   sta	 CTLSWA
	lda	 #I2C_SCL_MASK
	sta	 SWCHA
rts

;--------------------------------------------------------------------
;i2c_rxack - receiving an ack
;i2c_rxbit - receive a bit
;
; parameters:
;	none
;
; returns:
;	bit or ack in carry
;
; notes:
;	x and y are preserved
;--------------------------------------------------------------------
i2c_rxack:
i2c_rxbit:
	lda	 #$00
	sta	 SWCHA
	lda	 #I2C_SCL_MASK
	sta	 CTLSWA
	lda	 #I2C_SCL_MASK
	sta	 SWCHA
	lsr a
	lsr a
	lsr a		   ; C = SDA
rts

;--------------------------------------------------------------------
;i2c_startread - send eeprom start read command
;i2c_startwrite - send eprom start write command
;
; parameters:
;	none
;
; returns:
;	nothing
;
; notes:
;	x and y are preserved
;--------------------------------------------------------------------

i2c_startread:
clv
jsr i2c_start
	lda #$A1			; eeprom read command
	bne i2c_txbyte	 ; always
i2c_startwrite:
jsr i2c_start
	lda #$A0		   ; eeprom write command
	bne i2c_txbyte	 ; always

;--------------------------------------------------------------------
;i2c_txbyte - transmit a byte to the memory card
;
; parameters:
;	a - byte to transmit
;
; returns:
;	bit with ack
;
; notes:
;	y is preserved
;--------------------------------------------------------------------
i2c_txbyte:
	eor #$ff		   ; invert data byte
	sta memCardTemp
ldx #$08		   ; loop counter
@lp:	asl memCardTemp	; shift next bit into C
	jsr i2c_txbit	  ; transmit
	dex
	bne @lp
jmp i2c_rxack

;--------------------------------------------------------------------
;i2c_rxbyte - receive a byte from the memory card
;i2c_rxbyte
;
; parameters:
;	none
;
; returns:
;	a - byte received
;
; notes:
;	y is preserved
;--------------------------------------------------------------------
i2c_rxbyte:
ldx #$08		   ; loop counter
	bvc @skack		 ; previous byte needs acknowledge?
	jsr i2c_txack	  ; transmit acknowledge bit
@lp:	jsr i2c_rxbit	  ; receive bit in C
	rol memCardTemp	; rotate into scratchpad
	dex
	bne @lp
	lda memCardTemp	; get received byte from scratchpad
rts
@skack: bit @blah
@blah:  bvs @lp
i2c_stopread:
	bvc i2c_stopwrite
	jsr i2c_txnak	 ; transmit no-acknowledge
i2c_stopwrite:
jmp i2c_stop

Link to comment
Share on other sites

I think this is wrong:

i2c_rxack:
i2c_rxbit:
	lda	 #$00
	sta	 SWCHA
	lda	 #I2C_SCL_MASK
	sta	 CTLSWA
	lda	 #I2C_SCL_MASK
	sta	 SWCHA
	lsr a
	lsr a
	lsr a		 ; C = SDA
rts

I think you want to load SWCHA and then shift the bit into the carry.

 

Many thanks. You don't know how many times I have looked over the code and missed that.

 

--Ken

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...