Finding SmartPort Devices - FujiNetWIFI/fujinet-firmware GitHub Wiki

With the SmartPort Slot and Dispatcher found, devices can be enumerated and found using STATUS code $00.

C Implemenation

int8_t sp_find_fuji()
{
  // const char fuji[9] = "THE_FUJI";
  const char fuji[14] = "FUJINET_DISK_0";
  const uint8_t fuji_len = sizeof(fuji);
  int8_t err, num, i, j;

  err = sp_status(0x00, 0x00); // get number of devices
	if (err)
    return -err;
  num = sp_payload[0];
	num++;
	for (i = 1; i < num; i++)
	{
    //do
      err = sp_status(i, 0x03); // get DIB
    //while (err);
    if (sp_payload[4] == fuji_len)
    {
      for (j = 0; j < fuji_len; j++)
        if (fuji[j]!=sp_payload[5+j])
          return 0;
      sp_dest = i; // store the fuji unit #
      return i;
    }
	}
  return 0;
}

int8_t sp_find_network()
{
  const char net[7] = "NETWORK";
  const uint8_t net_len = sizeof(net);
  int8_t err, num, i, j;

  err = sp_status(0x00, 0x00); // get number of devices

  if (err)
    return -err;

  num = sp_payload[0];
  num+=2;

  for (i = 1; i < num; i++)
    {
      err = sp_status(i, 0x03); // get DIB

      if (sp_payload[4] == net_len)
	{
	  for (j = 0; j < net_len; j++)
	    if (net[j]!=sp_payload[5+j])
	      return 0;

	  return i;
	}
    }
  printf("NET NOT FOUND");
  cgetc();
  return 0;
}

Assembly Implemenation

FIND_DEVICE_BUF_LEN:    .BYTE 0
FIND_DEVICE_BUF:        .RES  255
relocate011:
FIND_DEVICE_BUF_ADDR:   .WORD FIND_DEVICE_BUF


;******************************************************************
; SP_STATUS
;   The Status command returns information about a specific device.
; The information returned by this command is determined by status code.
; On return from a Status call, the microprocessor X and Y registers are set to
; indicate the number of bytes transferred to the Apple II by the command. The X
; register is set to the low byte of the count, and the Y register is set to the high byte.
; The parameter list for this call is as follows:
; Byte Definition
;  0   parameter list length
;  1   unit number
; 2-3  status list pointer (lsb-msb)
;  4   status code
; INPUT
;   X - UNIT DESTINATION
;   Y - STATUS CODE
;       Y='S' return SP[0..1] = Bytes waiting, SP[2] & 0x02 = connected 
;******************************************************************
SP_STATUS:

                LDA #SP_STATUS_PARAM_COUNT
                STA CMD_LIST                    ; PARAMETER COUNT

                STX CMD_LIST+1                  ; X = DESTINATION DEVICE

                LDA SP_PAYLOAD_ADDR             
                STA CMD_LIST+2                  ; STATUS LIST POINTER LO
                LDA SP_PAYLOAD_ADDR+1    
                STA CMD_LIST+3                  ; STATUS LIST POINTER HI

                STY CMD_LIST+4                  ; Y = STATUS CODE

                JSR CALL_DISPATCHER

                .BYTE SP_CMD_STATUS             ; STATUS CALL COMMAND NUMBER
relocatesp07:   .WORD CMD_LIST

                STX SP_COUNT
                STY SP_COUNT+1
                STA LAST_SP_ERR
                
                BCC SP_STATUS_DONE

ERROR3:
                RTS
SP_STATUS_DONE:
                RTS


;*******************************
; SP_FIND_DEVICE
;   Looks for the specified smartport device
; INPUT
;   Put NULL terminated string of device to
;   search for in FIND_DEVICE_STR
; RETURNS
;   A = High byte address of string
;   Y = Low byte address of string
;   X = Device number or $FF on failure
;*********************************

SP_FIND_DEVICE:
                STA ZP1_HI                              ; STORE THE STRING ADDRESS
                STY ZP1_LO
                
                LDX #$00
                LDY #$00
LOOK_FOR_NULL:
                LDA (ZP1),Y                             ; START OF STRING WITHOUT LENGTH
                STA FIND_DEVICE_BUF,X                   
                BEQ GOT_LENGTH                          ; STOP WHEN WE GET TO NULL
                INY
                INX
                CLC
                BCC LOOK_FOR_NULL
GOT_LENGTH:     
                STX FIND_DEVICE_BUF_LEN                 ; SAVE THE LENGTH INCLUDES NULL

                LDX #$00
                LDY #SP_CMD_STATUS                      ; ASK FOR SMARTPORT STATUS
                JSR SP_STATUS

GOT_DEVICE_COUNT:
                LDX DCOUNT                              ; THE NUMBER OF DEVICES
                INX
                STX NUM_DEVICES
                LDX #$01                                ; START AT DEVICE #1

NEXT_DEV2: 
                TXA
                PHA 

                LDY #SP_STATUS_DIB                      ; X IS DEVICE 
                JSR SP_STATUS                           ; GET INFO
                BCS ERROR_OUT                           ; QUIT IF WE GET AN ERROR
                
                LDA SP_PAYLOAD+4                        ; LENGTH OF STRING
                CMP FIND_DEVICE_BUF_LEN                 ; IS IT THE SAME SIZE AS THE STRING WE'RE LOOKING FOR?
                BNE NEXT_DEVICE                         ; NOPE, CHECK NEXT DEVICE

                ; SAME SIZE STRING, NOW CHECK AND SEE IF IT
                ; IS THE DEVICE WE'RE LOOKING FOR

                LDX #$00        
SCAN_CHAR:

                LDA SP_PAYLOAD+5,X                      ; INFO STRING
                CMP FIND_DEVICE_BUF,X                   ; DEVICE WE'RE LOOKING FOR
                BNE NEXT_DEVICE                         ; NOT THE SAME, CHECK NEXT DEVICE

                INX                                     ; MOVE TO NEXT DEVICE
                CPX SP_PAYLOAD+4                        ; HAVE WE FINISHED LOOKING AT THE SAME NUMBER OF CHARACTERS?
                BNE SCAN_CHAR                           ; NOPE, KEEP GOING                           

                CLC
                BCC FOUND_DEVICE                        ; WE FOUND OUR DEVICE
NEXT_DEVICE:
                PLA                                     ; REMOVE THE DEVICE NUMBER OFF STACK
                TAX                                     
                INX                                     ; AND INCREMENT IT
                CPX NUM_DEVICES                         ; HAVE WE CHECKED ALL DEVICES?
                BNE NEXT_DEV2                           ; NOPE, KEEP GOING

                ; EXHAUSTED OUR LIST

                LDX #SP_ERR                             ; NOT FOUND
                LDA #SP_ERROR_NOT_FOUND
                CLC
                BCC FOUND_DONE

ERROR_OUT:      
                PLA
ERROR_OUT2:
                ; ERROR STRING HERE

                LDX #SP_ERR                               ; ERROR
                RTS

FOUND_DEVICE:
                PLA
                TAX

FOUND_DONE:    

                RTS