Guest User

Untitled

a guest
Jul 20th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.64 KB | None | 0 0
  1.  
  2. #include <kos.h>
  3. //#include <dream.h>
  4.  
  5. #include "ds.h"
  6.  
  7. #include "/usr/local/dc/kos/kos/kernel/arch/dreamcast/fs/dcload-syscalls.h"
  8.  
  9. int memzero(unsigned char *buf,int size)
  10. {
  11.     int i;
  12.     for(i=0;i<size;i++)
  13.         if (buf[i]) return 1;
  14.     return 0;
  15. }
  16.  
  17. #define N   1
  18.  
  19. #if 1
  20. #define open2 open
  21. #define close2 close
  22. #define write2 write
  23. #else
  24.  
  25. static int compress_fd = -1;
  26. static int zero_count;
  27. int open2(char *file,int flags)
  28. {
  29.     int fd = open(file,flags);
  30.     if (flags&0x80000000) {
  31.         compress_fd = fd;
  32.         zero_count = 0;
  33.     }
  34.     return fd;
  35. }
  36.  
  37. int write2(int fd,unsigned char *buf,long size)
  38. {
  39.     int i,ret;
  40.  
  41.     if (fd!=compress_fd) return write(fd,buf,size);
  42.  
  43.     if (memzero(buf,i)==0) {
  44.         zero_count+=size;
  45.     } else {
  46.         int count = 0x80000000 + size;
  47.         char save[4];
  48.         if (zero_count) {
  49.             ret = write(fd,&zero_count,4);
  50.             zero_count = 0;
  51.         }
  52.         buf-=4;
  53.         memcpy(save,buf,4);
  54.         buf[0]=count;
  55.         buf[1]=count>>8;
  56.         buf[2]=count>>16;
  57.         buf[3]=count>>24;
  58.         ret = write(fd,buf,size+4);
  59.         memcpy(buf,save,4);
  60.     }
  61.     if (ret!=-1) ret = size;
  62.     return ret;
  63. }
  64.  
  65. int close2(int fd)
  66. {
  67.     if (fd==compress_fd) {
  68.         compress_fd = -1;
  69.         if (zero_count) {
  70.             write(fd,&zero_count,4);
  71.             zero_count = 0;
  72.         }
  73.     }
  74.     return close(fd);
  75. }
  76. #endif
  77.  
  78. void track_save(int no,int first,int size,int type)
  79. {
  80.     int i,fd;
  81.     char file[40];
  82.     char secbuf[2352*N];
  83.     const static char data_hdr[12] = {
  84.         0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
  85.     };
  86. #if 1
  87.     int compress = 0;
  88. #else
  89.     int compress = 0x80000000;
  90. #endif
  91.     int prev,sizemb,secbyte;
  92.  
  93.     ds_printf(file,"track%02d.%s",no,type==4?"iso":"raw");
  94. //  strcpy(file,"tmp");
  95.     ds_printf("%s\n",file);
  96.     fd = open2(file,O_WRONLY|O_CREAT|O_TRUNC|compress);
  97.     if (fd<0) {
  98.         ds_printf("open err");
  99.         return;
  100.     }
  101.  
  102.     secbyte = (type==4?2048:2352);
  103.     sizemb = size*secbyte/1000000;
  104.  
  105.     prev = -1;
  106.     for(i=0;i<size;i+=N) {
  107.         int mega = i*secbyte/1000000;
  108.         if (prev!=mega) {
  109.             ds_printf(" %4d/%dM %d%\r",mega,sizemb,i*100/size);
  110.             prev = mega;
  111.         }
  112.         if (cdrom_read_sectors(secbuf,first+i,N)) {
  113.             ds_printf("read err");
  114.             close2(fd);
  115.             return;
  116.         }
  117.         if (type==4) {
  118.             /* data */
  119.             if (memcmp(secbuf,data_hdr,10/*sizeof(data_hdr)*/)){
  120.                 break;
  121.             }
  122.             write2(fd,secbuf+16,secbyte);
  123.         } else {
  124.             /* audio? */
  125.             if (memzero(secbuf,secbyte)==0) continue;
  126.             /* skip all zero */
  127.             write2(fd,secbuf,secbyte);
  128.         }
  129.     }
  130.     ds_printf(" %4d/%d %d%\r",sizemb,sizemb,100);
  131.     close2(fd);
  132. }
  133.  
  134. // 0x8c0000bc is the original DC BIOS GD_ROM Syscall address!!...
  135. #define MAKE_SYSCALL(rs, p1, p2, idx) \
  136.     uint32 *syscall_bc = (uint32*)0x8c0000bc; \
  137.     int (*syscall)() = (int (*)())(*syscall_bc); \
  138.     rs syscall((p1), (p2), 0, (idx));
  139.  
  140. /* Reset system functions */
  141. static void gdc_init_system() { MAKE_SYSCALL(/**/, 0, 0, 3); }
  142.  
  143. /* Submit a command to the system */
  144. static int gdc_req_cmd(int cmd, void *param) { MAKE_SYSCALL(return, cmd, param, 0); }
  145.  
  146. /* Check status on an executed command */
  147. static int gdc_get_cmd_stat(int f, void *status) { MAKE_SYSCALL(return, f, status, 1); }
  148.  
  149. /* Execute submitted commands */
  150. static void gdc_exec_server() { MAKE_SYSCALL(/**/, 0, 0, 2); }
  151.  
  152. /* Check drive status and get disc type */
  153. static int gdc_get_drv_stat(void *param) { MAKE_SYSCALL(return, param, 0, 4); }
  154.  
  155. /* Set disc access mode */
  156. static int gdc_change_data_type(void *param) { MAKE_SYSCALL(return, param, 0, 10); }
  157.  
  158.  
  159. int _cdrom_reinit(int n) {
  160.     int rv = ERR_OK;
  161.     int i, r = -1, cdxa;
  162.     uint32  params[4];
  163.  
  164.     /* Try a few times; it might be busy. If it's still busy
  165.        after this loop then it's probably really dead. */
  166.     for (i=0; i<8; i++) {
  167.         if (!(r = cdrom_exec_cmd(24, NULL)))
  168.             break;
  169.     }
  170.     if (i >= 8) { rv = r; goto exit; }
  171.    
  172.     /* Check disc type and set parameters */
  173.     gdc_get_drv_stat(params);
  174.     cdxa = params[1] == 32;
  175.     params[0] = 0;              /* 0 = set, 1 = get */
  176.     params[1] = (n?4096:8192);          /* 8192 ? */
  177.     params[2] = 0; //cdxa ? 2048 : 1024;        /* CD-XA mode 1/2 */
  178.     params[3] = (n?2352:2048);          /* sector size */
  179.     if (gdc_change_data_type(params) < 0) { rv = ERR_SYS; goto exit; }
  180.  
  181. exit:
  182.     return rv;
  183. }
  184.  
  185. /* The main program */
  186. int main(int argc, char **argv) {
  187.     int fd,i,ret;
  188.     int first,last,session;
  189.     char secbuf[2352];
  190.     char *p;
  191.  
  192.     CDROM_TOC toc;
  193.  
  194.     ds_printf("\nGD-Debug by OzOnE. 2011\n\n");
  195.  
  196.     if (cdrom_init() || _cdrom_reinit(1)) {
  197.         ds_printf("cdrom init error\n");
  198.         return CMD_ERROR;
  199.     }
  200.  
  201.     ret = cdrom_read_sectors(secbuf,45150,1);
  202.            
  203.     for (i=0; i<32; i++) {
  204.         ds_printf(" %01X", secbuf+i);
  205.     }
  206.        
  207.     if (ret) {
  208.         ds_printf("ip read error\n");
  209.         return CMD_ERROR;
  210.     }
  211.    
  212.     if (memcmp(secbuf+16,"SEGA",4)) {
  213.         ds_printf("not GD\n");
  214.         exit(1);
  215.     }
  216.  
  217.     return CMD_OK;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment