( Basic PCI Bus Access Functions              )
( Copyright (C) 2003 Stefan Reinauer          )
( ------------------------------------------- )
( Adapted from OpenBIOS by Charles Childers   )
( ------------------------------------------- )
( Important Notes:                            )
(   1) This code is GPL'd, so it's not        )
(      included in a default installation     )
(   2) I haven't tested it well enough yet!   )
(   3) It's probably incomplete or buggy      )
( ------------------------------------------- )

hex

( bus dev fn -- busdevfn )
: busdevfn
  7 and swap 
  1f and 3 << or
  swap 8 << or
  ;

( busdevfn reg -- reg addr )
: config-command
  dup -rot
  3 neg and 
  swap 8 << or 
  80000000 or
  ;

( busdevfn reg -- x )
: pci-c@
  config-command
  cf8 iol!
  3 and cfc + 
  io@
  ;

( busdevfn reg -- x )
: pci-w@
  config-command
  cf8 iol!
  2 and cfc + iow@
  ;

( busdevfn reg -- x )
: pci-l@
  config-command
  cf8 iol! 
  drop
  cfc iol@
  ;

( busdevfn reg val -- )
: pci-c!
  -rot config-command 
  cf8 iol! 
  3 and cfc + io!
  ;
  
( busdevfn reg val -- )
: pci-w!
  -rot config-command 
  cf8 iol! 
  2 and cfc + iow!
  ;

( busdevfn reg val -- )
: pci-l!
  -rot config-command 
  cf8 iol! 
  drop
  cfc iol!
  ;

decimal
