www.xbdev.net xbdev - software development
Friday December 14, 2018
home | about | contact | Donations

     
 

XBOX Programming

More than just a hobby...

 

Ejecting your DVD Rom ...scary stuff :)

 

This shows another example of using the SMBus on the xbox - this time though, where not flashing the LED's... where ejecting the cd rom :)  Of course have you ever ejected your cd rom while in game?  Yup it resets!  Of course, there is also a set of instructions to allow you to eject while your xbe runs... you can look into this further.

An excellent reference for information on the SMBus is with the "XKUtils Source Code" which you can download, and shows the commands that you need to send to your SMBus to get the required actions that you want.

 

 

Code that where interested in:

I've just put the whole code together in the header.asm file, which you compile, but the code in asm, that does the ejecting, is shown below

 

Assembly: test.asm

eject_cd:

 

      mov dx, 0xc004

      mov al, 0x20

      out dx, al

                 

      mov dx, 0xc008  ; command

      mov al, 0x0C

      out dx, al

 

 

      mov dx, 0xc006  ; data

      mov al, 0x0

      out dx, al

 

      mov dx, 0xc000

      in ax, dx

 

      out dx, al

      mov dx, 0xc002

      mov al, 0x1a

      out dx, al

 

delay:

      mov eax, 1000000 ; Small delay loop

loopx:

      dec eax

      jne loopx

 

 

 

aa:

      jmp aa

 

Of course, there are people out there that curse at the thought of assembly.... *yuk*...who needs assembly...hehe... well there are ways around it if you use a compiler..either using inline asm, or with the pre-made API's

 

Inline C Code with ASM.

void funEject()

{

      _asm

      {

                  mov dx, 0xc004

                  mov al, 0x20

                  out dx, al

                 

                  mov dx, 0xc008   // command

                  mov al, 0x0C

                  out dx, al

 

 

                  mov dx, 0xc006  // data

                  mov al, 0x0

                  out dx, al

 

                  mov dx, 0xc000

                  in ax, dx

 

                  out dx, al

                  mov dx, 0xc002

                  mov al, 0x1a

                  out dx, al

 

                  mov eax, 1000000 // Small Delay loop

            loopx:

                  dec eax

                  jne loopx

      }

};

 

extern "C" XBOXAPI LONG WINAPI HalWriteSMBusValue(UCHAR devddress, UCHAR offset, UCHAR writedw, DWORD data);

 

EjectAPI()

{

      HalWriteSMBusValue(0x20, 0x0C, false, 0x00) ;

}

 

void __cdecl main()

{

      // Using method -1- of inline assembly

      funEject();

 

      // Using method -2- of API's

      EjectAPI();

}

 

Whole code, and nothing but the code.

 

Here is what the whole code looks like... of course its almost exactly the same as tutorial -2- where we generated an xbe, and made your xbox into a disco light show...  :)

 

Assembly: DownloadSource

; About? - Generates a simple xbe, which can be generated using the nasm

;          assembler!  100% yours!

; Description: Simple of the simplest programs, that demonstrates the ejecting

;          of the dvd rom

 

 

[ORG 0x10000]

[BITS 32]

 

; assemble using 'nasm' assembler

; C:>nasm header.asm -o simple.xbe

 

; XBE HEADER PART ------------------------------------------------------------

 

db    'XBEH'

resb    0x100           ; reserve 100 bytes for the xbe security signature

                        ; This only matters on signed xbe's which are

                        ; ms authorised :)

 

dd    0x10000         ; Base address of image

dd    0x760           ; Size of header

dd    0x7000            ; Size of image

dd      0x178           ; Size of image header

dd    0x3f16d3ce  ; Time Date Stamp

 

dd    cert_header ; Certificate Address (plus offset)

 

 

dd    0x1         ; Number of sections

dd    section_header    ; Sectrion headers Address(plus offset)

 

dd    0x1         ; Initialisation Flags

dd    0x11100 ^ 0xA8FC57AB   

                        ; Entry point (XOR with 0xA8FC57AB)

                        ; ***** Very important - this will be our entry

                        ; point for our code - which is the base address of

                        ; our file added to the actual file offset.

 

dd    0x16000           ; Thread local storage directory address

dd    0x0         ; Size of stack commit

dd    0x0         ; Size of heap reserve

dd    0x0         ; Size of heap commit

 

 

dd    0x0         ; Original base address

dd    0x0         ; Original size of image

dd    0x0         ; Original checksum

dd    0x3f16d3ce  ; Original time date stamp

dd    debugpath   ; Debug path name address

dd    debugpath

dd    szname

 

dd    0x11000 ^ 0x5B6D40B6         

                        ; Kernel image thunk address

                        ; We XOR the original address with 0x5B6D40B6

dd    0x0         ; Non-kernel import directory address

dd    0x0         ; Number of library versions

dd    0x0         ; Library versions area addresses

dd    0x3         ; Kernel library version address

dd    0x0         ; XAPI library version address

dd    0           ; Logo bitmap address

dd    0             ; Logo bitmap size

                        ; Not included a logo bitmap for this simple example

                        ; and will still run without it, but you could encode

                        ; a really cool image logo for you app maybe :)

 

 

; Note: - a lot of assemblers use "[ORG 0x354]" to align data within the assembled

;         file, but for nasm, you have to use: TIMES 0x178-($-$$), which implies

;         that everything from this line is from 0x178 in the file.

 

 

; XBE CERTIFICATION ----------------------------------------------------------

 

TIMES 0x178-($-$$) DB 0 ; I've aligned the certificate to exactly 0x178 in

                        ; the file, but you could exclude this if you want.

 

cert_header:

 

dd    0x1dc       ; Size of Certification

dd    0x3f16d3ce  ; Date Stamp

dd    0x0         ; Title ID

resb    0x50            ; Title name null terminated string

resb  0x40        ; Alt Title

dd    0x0         ; Allowed Media

dd    0x0         ; Game Region

dd    0x0         ; Game Rating

dd    0x0         ; Disk Number

dd    0x0         ; version

resb    0x10            ; Lan

resb  0x10        ; Signature Key

resb  0x100       ; Alt Sig

 

 

; SECTION HEADERS ------------------------------------------------------------

TIMES 0x354-($-$$) DB 0 ; Similar to the xbe cert header, I've aligned the

                        ; single section header to offset 0x354 exactly, but

                        ; you don't have to.

 

section_header:

 

dd    0x07        ; Flags

dd    0x11000           ; Virtual Address (remember offset of 0x10000)

dd    0x6000            ; Virutal Size

dd    0x1000            ; File pointer to raw data

dd    0x6000            ; Size of raw data

dd    section_name      ; Address of the section name (Null terminated)

dd    0x0         ;

dd    rc1         ; head_count_address

dd    rc2         ; tail_count_address

 

 

debugpath:

      db 0x0,0x0,0x0,0x0,0x0,0,0,0,0,0,0,0,0,0,0

szname:

      dw 'x','b','d','e','v',0,0,0

 

rc1:

  dd 0

rc2:

  dd 0

 

section_name:

  db '.','t','e','x','t',0,0

 

 

; 'OUR CODE'

; THE SECTION WE DEFINED -----------------------------------------------------

 

; This is the start of our section.. the start of our simple

; code.

; Its offset in the file is at 0x1000

TIMES 0x1000-($-$$) DB 0

 

section_1_start:        ; this should be 0x11000 in memory when loaded

; kernel thunk table

MmAllocateContiguousMemoryEx:

      dd    0x80000000 + 166

MmGetPhysicalAddress:

      dd    0x80000000 + 173

NtAllocateVirtualMemory:

      dd    0x80000000 + 184

 

      dd    0           ; end of table

 

; Of course you don't really need to include these few lines yet... but you'll

; need them later for when you start allocating memory - referenced from the

; xbox-linux-project.

                 

 

; Entry point of our code - offset 0x1100 in the file.

TIMES 0x1100-($-$$) DB 0x90   ; this should be 0x11100 in memory when loaded

 

 

; EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT

; EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT ** EJECT

 

; Well flashing LED's is one thing....so I added another small test routine,

; which is 100% asm... and allows you to eject your cd rom :)

 

; Here is what the definition of XDK API looks like, which you could also add into

; instead of doing it in 100% asm :)

 

; extern XBOXAPI LONG WINAPI HalWriteSMBusValue(UCHAR devddress, UCHAR offset, UCHAR writedw, DWORD data);

; HalWriteSMBusValue(SMBDEV_PIC16L, PIC16L_CMD_EJECT, 0, 0x00);

; SMBDEV_PIC16L = 0x20

; PIC16L_CMD_EJECT = 0x0C

 

 

eject_cd:

 

      mov dx, 0xc004

      mov al, 0x20

      out dx, al

                 

      mov dx, 0xc008  ; command

      mov al, 0x0C

      out dx, al

 

 

      mov dx, 0xc006  ; data

      mov al, 0x0

      out dx, al

 

      mov dx, 0xc000

      in ax, dx

 

      out dx, al

      mov dx, 0xc002

      mov al, 0x1a

      out dx, al

 

delay:

      mov eax, 1000000 ; Small delay loop

loopx:

      dec eax

      jne loopx

 

 

 

aa:

      jmp aa

 

 

; END OF EJECT ** END OF EJECT ** END OF EJECT ** END OF EJECT ** END OF EJECT

; END OF EJECT ** END OF EJECT ** END OF EJECT ** END OF EJECT ** END OF EJECT

 

 

 

TIMES 0x6000-($-$$) DB 0x90   ; this will make our section finish at 0x6000

                                ; from the start of the file.

 

TIMES 0x7000-($-$$) DB 0x0    ; And of course, this will make our file size

                                ; equal to 0x7000 a nice round number -

                                ; 0x7000 is 28672bytes... so if you assemble the file

                                ; you should find its that size exactly.

 

 

Hmmm.... all that header stuff is starting to get long... so I think we should split our code up into two parts. now.... well for the next tutorial... make a header.asm and a code.asm... then when we compile code.asm... just have it include all the header information :)

 

Soon we'll be putting graphics to the screen... rotating cube's.... hold on to your chair! ... its going to be fun *grin*

 

Pixels Tutorial Coming Soon :)

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.