bytetolba.c 1.4 KB

#include <ultra64.h>
#include "../org/include/leodefine.h"
#include "../org/include/leodrive.h"

#ifndef _LONGCMD
#include <leo.h>
#else
#include <leosp.h>
#endif

extern  s32     __leoActive;

/* export from leo_tbl.c */
extern 	u16 	LEOVZONE_TBL[DISK_TYPES][VZONES_PER_DRV];
extern 	u16 	LEOBYTE_TBL2[ZONES_PER_DRV];
extern 	u8  	LEOVZONE_PZONEHD_TBL[DISK_TYPES][VZONES_PER_DRV];

extern 	u8  	LEOdisk_type;

extern 	u16   	leoLba_to_vzone(u32 lba);


s32 LeoByteToLBA(s32 startlba, u32 nbytes, s32 *lba)
{
  u32 reslba;
  u32 byte_p_blk;
  u16 zone,vzone;
  u8  flag;

  /* 
   * Check for existence of Leo Manager 
   */
  if (!__leoActive) {
    return(-1);
  }
  
#ifdef _DEBUG
  if (startlba > MAX_L_LBA)
    return LEO_ERROR_LBA_OUT_OF_RANGE;

#endif

  reslba = 0;
  flag = vzone = 1;

  /*
   * Add system blocks
   */
  startlba += 24;
  
  while (nbytes)
  {
    if (flag
	|| (startlba == LEOVZONE_TBL[LEOdisk_type][vzone]))
    {
      vzone      = leoLba_to_vzone(startlba);
      zone       = LEOVZONE_PZONEHD_TBL[LEOdisk_type][vzone];
      if (zone > (u8)7)
	zone -= (u8)7;
      byte_p_blk = LEOBYTE_TBL2[zone];
    }

    if (nbytes < byte_p_blk)
      nbytes = 0;
    else
      nbytes -= byte_p_blk;

    reslba++;
    startlba++;

    if ((nbytes) && (startlba > (u32)MAX_P_LBA))
      return LEO_ERROR_LBA_OUT_OF_RANGE;
	      
    flag = 0;
  }
  *lba = reslba;

  return LEO_ERROR_GOOD;

} /* LeoByteToLBA() */