; --------------------------------------------------------------------------------
; @Title: Linux Demo for TRACE32 OS Awareness
; @Description: 
;   
;   This batchfile demonstrates the use of the OS Awareness for Linux
;   
;   The example is generated for an APM ASPEN SMP board using an ICD.
;   It will NOT run on any other board, but may be used as a template
;   for others.
;   
; @Keywords: aspen, awareness, linux, RTOS
; @Author: DIE
; @Board: APM-ASPEN-SMP
; @Chip: APM86692
; @Copyright: (C) 1989-2014 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: linux_attach.cmm 13987 2019-02-19 16:15:20Z kjmal $


; set CONFIG_BDI_SWITCH to prevent Linux accessing debug registers

; Starting Linux example with TRACE32 and Hyperterminal:
; - Connect NULL modem cable to PORT A (upper jack)
; - Start TRACE32
; - Open Hyperterminal: 57600 baud, 8/N/1, no(!) handshake
; - (T32) do linux
; - (HT) login as root and start "hello"



 LOCAL &srcdrive
 LOCAL &srcpath
 &srcdrive="X:"
 &srcpath="&srcdrive"+"\"
 &symboldrive="Z:"
 &symbolpath="&symboldrive"+"\aspen"


 SCREEN.ON


; Debugger Reset

 WinPAGE.RESet
 AREA.RESet
 WinPOS 0. 23. 70. 9. 0. 0. W000
 AREA.view
 
 PRINT "resetting..."

 SYStem.Down
 TASK.RESet
 Break.RESet
 MAP.RESet
 sYmbol.RESet
 TRANSlation.RESet
 GROUP.RESet
 
 

; Initializing Debugger

 PRINT "initializing..."
 SYStem.CPU APM86692
 CORE.NUMber 1.
 SYStem.Option MMUSPACES ON     ; enable space ids to virtual addresses
 SYStem.Up
 Register.RESet
 SETUP.IMASKASM ON              ; lock interrupts while single stepping
 

; Target Setup: initialize board memory access and peripherals

 ; let the boot monitor setup the board
 Break.Set 0x0 /Onchip
 Go.direct
 PRINT "target booting..."
 WAIT !run() 30.s
 IF run()
 (
   Break.direct
   PRINT %ERROR "target boot failed..."
   ENDDO
 )
 Break.Delete 0x0
  

; Load the Linux kernel

; Load the Linux kernel symbols into the debugger
 ; /strippart and /path are used to map source paths
 
 PRINT "loading Linux kernel symbols..."
 &path="&symbolpath"+"\vmlinux"
 Data.LOAD.Elf &path /NoCODE /NoRegister /StripPART 3. /PATH "&srcpath" 
 

; Open a Code Window -- we like to see something

 WinPOS 0. 0. 75. 20.
 List.auto
 SCREEN.display
 

; Go until MMU initialization

 Go.direct MMU_init /Onchip
 PRINT "initializing target MMU..."
 WAIT !run()


; Declare the MMU format to the debugger
 ; - table format is "LINUXEXT"
 ; - table base address is at label "swapper_pg_dir"
 ; - kernel address translation
 ; Map the virtual kernel symbols to physical addresses to give 
 ; the debugger access to it before CPU MMU is initialized

 PRINT "initializing debugger MMU..."
 MMU.FORMAT LINUXEXT swapper_pg_dir 0xc0000000--0xefffffff 0x00000000
 TRANSlation.Create 0xc0000000--0xefffffff 0x00000000   ; map kernel pages at RAM start
 TRANSlation.COMMON 0xc0000000--0xffffffff              ; common area for kernel and processes
 TRANSlation.TableWalk ON   ; debugger uses a table walk to decode virtual addresses
 TRANSlation.ON             ; switch on debugger(!) address translation 
 

; Initialize Linux Support

 ; Note that the Linux awareness needs the kernel symbols to work
 
 PRINT "initializing Linux support..."
 ; Choose the correct Linux major version below
 TASK.CONFIG ~~/demo/powerpc/kernel/linux/linux-2.x/linux.t32     ; loads Linux awareness for linux-2.x
 MENU.ReProgram ~~/demo/powerpc/kernel/linux/linux-2.x/linux.men  ; loads Linux menu  for linux-2.x
 ;TASK.CONFIG ~~/demo/powerpc/kernel/linux/linux-3.x/linux3.t32     ; loads Linux awareness for linux-3.x
 ;MENU.ReProgram ~~/demo/powerpc/kernel/linux/linux-3.x/linux.men  ; loads Linux menu  for linux-3.x


 ; Group kernel area to be displayed with red bar
 GROUP.Create "kernel" 0xc0000000--0xffffffff /RED

; Ok, we're done, let's start Linux
 Go.direct
 PRINT "starting Linux... (please wait)"
 WAIT 4.s
 Break.direct
 
 PRINT "done."     ; done with loading and starting Linux
 
 
; -------------------------------------------------------------------------------- 
;       Application Debugging 
;           e.g. "hello" 
; --------------------------------------------------------------------------------

 ; activate autoloader for processes:
 TASK.sYmbol.Option AutoLoad Process

 PRINT "please log in and start 'hello'"

 ; script based
   ; using the script based application debugging
   ; gives more control over the single commands
   ; see file "app_debug.cmm"
   
   ;LOCAL &extdir
   ;&extdir=TASK.GETDIR()
   ;DO &extdir/app_debug hello
   
 ; process watch system
   ; the process watch system automates the actions
   ; for application debugging
   
   WinPOS 55. 25. 40. 4.
   TASK.Watch.View "hello"

   Go.direct

 ENDDO
