; --------------------------------------------------------------------------------
; @Title: Linux Demo for TRACE32 OS Awareness on the ARM Versatile
; @Description: 
;   This batchfile demonstrates the use of the OS Awareness for Linux
;   The example is generated for an ARM Versatile board using an ICD.
;   It will NOT run on any other board, but may be used as a template
;   for others.
;   Linux is downloaded to the board via ICD.
; @Keywords: awareness, RTOS, Versatile
; @Author: DIE
; @Board: Versatile
; @Chip: ARM926EJ-S
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: versatile_linux.cmm 15236 2019-11-08 07:27:03Z bschroefel $


; Starting Linux example with TRACE32:
; - Connect terminal (NULL modem cable) to UART0
;   - 38400 baud, 8/N/1, no(!) handshake
; - Start TRACE32
; - Switch on the Versatile board
; - (T32) do linux
; - start "hello"

 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. 25. 75. 7. 0. 0. W000
 AREA.view
 
 PRINT "resetting..."

 SYStem.Down
 TASK.RESet
 Break.Delete
 MAP.RESet
 sYmbol.RESet
 TRANSlation.RESet


; Initializing Debugger
 
 PRINT "initializing..."
 SYStem.CPU ARM926EJ
 SYStem.Option DACR ON          ; give Debugger global write permissions
 SYStem.Option ResBreak ON      ; hardware dependent (see manual)
 SYStem.Option BigEndian OFF    ; this demo is in little endian
 TrOnchip.Set DABORT OFF        ; used by Linux for page miss!
 TrOnchip.Set PABORT OFF        ; used by Linux for page miss!
 TrOnchip.Set UNDEF OFF         ; my be used by Linux for FPU detection
 SYStem.Option MMUSpaces ON     ; enable space ids to virtual addresses
 
 IF etm()
 (
   ETM.PortSize 8        ; ETM port size 8 bit
   ETM.DataTrace OFF     ; no data trace
   ETM.ContextID 32      ; trace context id (if used by Linux)
   ETM.ON                ; switch ETM trace on
 )
 
 SYStem.Up
 Register.RESet

 SETUP.IMASKASM ON          ; lock interrupts while single stepping


; Open serial terminal window on COM1
 
 DO ~~/demo/etc/terminal/serial/term.cmm COM1 38400.
 

; Target Setup: initialize DRAM controller and peripherals
 
 ; Either let the boot monitor setup the board
    Go
    PRINT "target setup..."
    WAIT 2.s
    Break
 ; or use the debugger to initialize it
    ;print "target setup..."
    ;do versatile   ; do basic setup


; Load the Linux kernel

 ; 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 (loading the kernel, setting registers
 ; and boot parameters) only to load the kernel into
 ; RAM using the debugger.

 ; vmlinux starts physically at RAM start (=0x0) + 0x8000 
 ; We have to adjust it from the virtual start address at the label
 ; "stext" from the System.map ("nm vmlinux | sort")
 ; i.e.: Data.LOAD.Elf vmlinux <physical start>-<virtual start>

 PRINT "loading Linux image..."
 Data.LOAD.Elf vmlinux 0x00008000-0xc0008000
 
 Register.RESet

 ; Set PC on start address of image
 Register.Set PC 0x8000
 
 ; Set machine type in R1; see arch/arm/tools/mach-types
 ;Register.Set R1 387.    ; Versatile PB
 Register.Set R1 606.    ; Versatile AB

 ; Set up the passed command line
 
 Var.set default_command_line = "mem=32M console=ttyAMA0,38400 initrd=0x00800000,0x200000 root=/dev/ram"


; Loading initial ram disk (initrd)

 ; Use the next lines only, if you want to use an initrd,
 ; and if you want to download this with the debugger.

 ; Load rom file system image into ram disk
 ; The load address is either hardcoded in arch/arm/<board>/arch.c,
 ; or must be passed by a boot parameter, or as command line option
 
 PRINT "loading ram disk"
 Data.LOAD.Binary ramdisk.image.gz 0x00800000 /NoClear /NosYmbol
 
; Load the Linux kernel symbols into the debugger
 ; use /strippart and /path to map source paths, if necessary
 
 PRINT "loading Linux kernel symbols..."
 Data.LOAD.Elf vmlinux /gnu /NoCODE /StripPART "/mnt/develop/" /PATH "S:\"


; Open a Code Window -- we like to see something

 WinPOS 0. 0. 75. 20.
 List.auto
 SCREEN.display
 
  
; Run over MMU & Interrupt initialization

 ; use onchip, because we're passing MMU initialization
 Go __init_end /Onchip 
 PRINT "running Linux initialization..."
 WAIT !STATE.RUN()
 

; 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--0xc1ffffff 0x00000000
 TRNASlation.COMMON 0xbf000000--0xffffffff            ; common area for kernel and processes
 TRANSlation.TableWalk ON   ; debugger uses a table walk to decode virtual addresses
 TRNASlation.ON             ; switch on debugger(!) address translation 
 

; Initialize Linux Awareness

 ; 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/arm/kernel/linux/linux-2.x"
     &awareness="linux.t32"
 )
 ELSE
 (
     &ka_path="~~/demo/arm/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 
  
 
 IF etm()
   TASK.Option THRCTX ON      ; for thread enabled context id
 
 
 ; Group kernel area to be displayed with red bar
 GROUP.Create "kernel" 0xc0000000--0xffffffff /RED


; Ok, we're done, let's start Linux
 
 wintop TERM.view        ; set terminal window to front
 Go
 PRINT "starting Linux... (please wait)"
 WAIT 4.s
 Break
 
 PRINT "done."
 
 
; --------------------------------------------------------------------------------
; 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"
   
   ; do ~~/demo/arm/kernel/linux/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

 ENDDO
