#------------------------------
#--    Lec-1-HW-2-OSprelims  --
#------------------------------

#============================================================
#== Part I, set up. =========================================
#============================================================

#-- NOTE: This is written using some "make" syntax, exactly
#-- as if this was a Makefile. This is so (1) you will get used 
#-- to the syntax, (2) you can use some of this in your own Makefile 
#-- if you  care to, (3) there is less ambiguity about what I mean.
#-- You can copy/paste this into your own Makefile and use it,
#-- or you can simply read it and follow instructions.

#-- Here is a variable, URL, it contains a string. Using the
#-- variable "$(URL)" means replacing it with its string.
URL=https://svn.cs.georgetown.edu/svn

CRS=121-2013
AUTH=--username 250-374-developer --password 'y(&qwqsq'

#-- Change these variables as appropriate.
HOME=~
ME=rks

#-- The first thing to do is get your branch's trunk set up.
#-- (A) Create your branch and trunk.

#-- (A.1) get a copy of branches/

coProjects::
	cd $(HOME); svn co $(URL)/projects2/$(CRS)/branches $(AUTH)

#-- (A.2) Create your branch and trunk, add them to repository.

mkTrunk::
	cd $(HOME)/branches; \
	svn mkdir $(ME);  \
	cd $(ME); \
	svn mkdir trunk; \
	cd .. ; \
	svn ci

#-- (A.3) Destroy your copy of branches/ and checkout your branch.

rmBranches::
	cd $(HOME); \
	/bin/rm -rf branches; \
	svn co $(URL)/projects2/$(CRS)/branches/$(ME) $(AUTH)
	

#-- (B) Set up your trunk/

#-- (B.1) Check out src/ (a nested checkout) into your trunk

coSrc:: 
	cd $(ME)/trunk; \
	svn co $(URL)/projects/LC3trunk/src $(AUTH)
	
#-- (B.2) Use src/ to populate your trunk.

mkDirs::
	cd $(ME)/trunk/src; \
	make INSTALL

#-- cd to $(ME)/src and follow the directions.

#-- (B.3) When done with that, destroy the nested checkout of src/

rmSrc::
	cd $(ME)/trunk; \
	/bin/rm -rf src/

#-- If you need anything from src/, you can get another copy later.


#============================================================
#== Part II, Coding exercise ================================
#============================================================

The last part of this file contains .asm code. This code is the
seed of our OS project.  Copy/paste it, saving it as "os0.asm" in
your src2/. Be careful not to include the html tags at the bottom.

The code includes "?" where I have deleted parts of
the instructions. You are to replace the missing parts. This code
is just the OS's enty preamble and the OS's initialization section.
There is a global data table, which needs to have the Global Data
Pointer (GDP, R4) pointing to it. This is taken care of in the
preamble. The code then jumps to the initialization. Here the
supervisor's stack is set up. That is, SP is initialized to point
to the memory location that is the bottom of the supervisor's
stack. The address for that is a constant in the global data table.

Work cycle: Comment out the code with "?" in it. Run the assembler,
lc3as, to see that you are staring with a clean basis. Then fix an
instruction and try assembling again. If that works, load the .obj 
into PennSim to test it. Single step, checking that the registers 
are changing correctly. Once that code is working, repeat the cycle.

NB--PennSim includes a built-in assembler. You must have PennSim.jar
in the same directory as your .asm file, and start it there. There is
a command window just below the row of buttons "Next Step ... ".
This command typed into that window will run the assembler:
    as foo.asm
You can then load foo.obj using the File menu.

Do not use LD, LDI, ST, STI, JSR, JSRR, or TRAP instructions. These 
can all be implemented using other instructions. Use the GDP to
access the global data. Jumps should be done using JMP, not BR.

What to turn in on paper: 
     name, course, year, HW title;
     comments on how your progress went, problems with tools, etc.;
     your .asm source code solution.

What to check in to you branch:
     your source code;
     test files, if any;
     comment text files, if any;
     etc.

PS: For fun, you can write into LC3's video ram (VRAM). PennSim's version
of the LC3 has a memory-mapped VRAM. The display controller reads the
VRAM and displays it: Each word contains a color code for one screen
pixel. The display is shown under "Devices" in the lower part of PennSim's
window. VRAM starts at address xC000 and goes from there to the I/O 
registers. In PennSim, change the value of any memory word in VRAM and 
see what the result is in the display window. The color is coded as a
red-green-blue triple (RGB), each base color's intensity is a 5-bit
value: word[4:0] for blue intensity, word[9:5] for green, and word[15:10]
for red.


        .ORIG x0200
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; os0.asm
;;;
;;; Contents:
;;;   OS text segment (executable code)
;;;      Preamble
;;;      Boot Initialization
;;;   OS data segment (constants and variables)
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;==================================================
OS_CODE:    ;======== OS TEXT SEGMENT begin =======
;==================================================

;-------------------------------------------------
OS_BEGIN:   ;-- OS entry (x0200)
;-------------------------------------------------

  ;---------------------------------
  ;-- Preamble:
                             ;-- point GDP (R4) to OS_DATA:
  lea r7, ???????????        ;--    R7  <== pointer's address;
  ldr r4, ???????????        ;--    GDP <== pointer's value

                             ;-- Jump to OS initialization
  lea ?????????????????????  ;--    R7 <== pointer's address;
  ldr ?????????????????????  ;--    R7 <== pointer's value;
  ????????                   ;--    jump (no return)

OS_DATA_PTR:        .FILL OS_DATA          ;-- Preamble data
OS_init_BEGIN_PTR:  .FILL OS_init_BEGIN    ;-- Preamble data


;-------------------------------------------------
OS_init_BEGIN:   ;-- OS boot/initialization
;-------------------------------------------------

  ;---------------------------------------------------
  ;-- Initialize the supervisor's stack.
  ;-- Set SP (R6) to start of user space, set BP (R5)
  ;-- to be the same as SP.

  ldr r6, r4, ????   ;-- Set the SP to stack bottom.
  add ????????????   ;-- BP <== SP

;==================================================
OS_CODE_END:    ;====== OS TEXT SEGMENT end =======
;==================================================



;;;================================================
OS_DATA:      ;;;====== OS DATA SEGMENT begin =====
;;;================================================

;;;------------------------------------------------
;;;-- OS Constants and Data Structures
;;;-- These are constants or memory for variables used
;;;-- by the OS. GDP points here when OS code is
;;;-- executing. User code sets GPD to its data segment.
;;;------------------------------------------------

  SUPER_STACK:      .FILL x3000                   ;-- (offset=0)
  USER_CODE:        .FILL x3000                   ;-- (offset=1)
  USER_STACK:       .FILL xFE00                   ;-- (offset=2)

;;;================================================
OS_DATA_END:  ;;;===== OS DATA SEGMENT end ========
;;;================================================

                .END