; --------------------------------------------------------------------------------
; @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 ML403 board using an ICD.
;   It will NOT run on any other board, but may be used as a template
;   for others.
;   
; @Keywords: awareness, linux, ml403, RTOS
; @Author: DIE
; @Board: ML403
; @Chip: PPC440
; @Copyright: (C) 1989-2015 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: linux.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 console terminal:
; - Connect NULL modem cable to UART
; - Switch on debugger and board
; - Start TRACE32 for PowerPC
; - Start TRACE32 for MicroBlaze
; - Open console terminal: 9600 baud, 8/N/1, no(!) handshake
; - TRACE32 for PowerPC: "do linux"
; - wait for dialog to start MicroBlaze
; - TRACE32 for MicroBlaze: "do mbprime"
; - Press "OK" in dialog
; - terminal: after Linux booted, start "sh home/driver.sh"

; This example runs an application, that communicates to the
; MicroBlaze via a driver. At the end of this script, it sets
; two breakpoints: one in the Linux application, and one in
; the driver routine.

 LOCAL &ka_path &awareness

 SCREEN.ALways      ; permanent update for internal terminal window
 ; screen.on        ; if you use external terminal


; Debugger Reset

 WinPAGE.RESet
 AREA.RESet
 WinPOS 0. 23. 70. 8. 0. 0. W000
 AREA.view
 
 PRINT "resetting..."
 
 RESet
 

; Initializing Debugger

 PRINT "initializing..."
 SYStem.CPU VirtexPPC

 ; configure JTAG chain for PowerPC
 SYStem.CONFIG.IRPOST 24.
 SYStem.CONFIG.IRPRE   8.
 SYStem.CONFIG.DRPOST  2.
 SYStem.CONFIG.DRPRE   1.
 SYStem.CONFIG.TRISTATE OFF
 
 SYStem.BdmClock 10mhz          ; set BDM clock speed

 JTAG.LOADBIT system.bit        ; load the FPGA design
 
 SYStem.Option MMUSPACES ON     ; enable space ids to virtual addresses

 SETUP.IMASKASM ON              ; lock interrupts while single stepping

 SYStem.Up
 

; Open serial terminal window on COM1
 
 TERM.RESet
 TERM.METHOD COM COM1 9600. 8 NONE 1STOP NONE 
 TERM.Mode VT100
 TERM.SIZE 80. 1000.
 TERM.SCROLL ON
 WinPOS 35. 0. ,,,,, TERM.view
 TERM.view
 

; Flush the caches before downloading Linux!
 CACHE.FLUSH DC
 CACHE.FLUSH IC
 
 
; Load the Linux image

 ; If you are using a flashed kernel, or if you're loading 
 ; your kernel via TFTP, use the boot monitor to do so.
 
 ; Use the next lines only when loading the Linux
 ; image into RAM using the debugger.

 Register.RESet

 PRINT "loading Linux kernel..."
 Data.LOAD.Elf simpleImage.initrd.virtex405-ml403.elf /StripPART 3 /SOURCEPATH ../ml507/linux-2.6-xlnx.git/arch/powerpc/boot/

 PRINT "loading Linux kernel symbols..."
 Data.LOAD.Elf vmlinux /gnu /NoCODE /NoClear /StripPART 3 /SOURCEPATH ../ml507/ 


; Open a Code Window -- we like to see something

 WinPOS 0. 0. 75. 20.
 List.auto
 SCREEN.display
 

; Declare the MMU format to the debugger
 ; - table format is "LINUX"
 ; - 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 LINUX swapper_pg_dir 0xc0000000--0xc3ffffff 0x00000000
 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
 
 ; check linux major version
 IF STRing.SCAN(Data.STRing(linux_banner), "Linux version 2.", 0)==0
 (
     &ka_path="~~/demo/powerpc/kernel/linux/linux-2.x"
     &awareness="linux.t32"
 )
 ELSE
 (
     &ka_path="~~/demo/powerpc/kernel/linux/linux-3.x"
     &awareness="linux3.t32"  
 )
 
 PRINT "initializing RTOS support..."
 TASK.CONFIG &ka_path/&awareness       	; loads Linux awareness 
 MENU.ReProgram &ka_path/linux    	; loads Linux menu 
  

 ; Group kernel area to be displayed with red bar
 GROUP.Create "kernel" 0xc0000000--0xffffffff /RED


; now start the MicroBlaze
 DIALOG.OK "Please start MicroBlaze"
 

; Ok, we're done, let's start Linux

 wintop TERM.view        ; set terminal window to front
 Go.direct
 PRINT "starting Linux... (please wait)"
 WAIT 20.s
 Break.direct
 
 PRINT "done."     ; done with loading and starting Linux


; -------------------------------------------------------------------------------- 
;       Application Debugging 
;           e.g. "a.out" 
; --------------------------------------------------------------------------------

 ; activate autoloader for processes and kernel modules:
 TASK.sYmbol.Option AutoLoad Process
 TASK.sYmbol.Option AutoLoad Module

 PRINT "please start 'sh home/driver.sh'"

 ; wait for driver.sh starting a.out
   LOCAL &extdir
   &extdir=TASK.GETDIR()
   DO &extdir/app_debug a.out

 ; set a breakpoint within the while loop
 Break.Set main\14   
 
 ; load the symbols of the driver (kernel module)
 sYmbol.AutoLOAD.TOUCH "mydriver"

 ; set a breakpoint into a driver function
 Break.Set driver_read

 ; start application 
 Go.direct
   
 PRINT "done; dangling breakpoints set."

 ENDDO
