/*
    SupportAsm.S, Copyright  (c) by George Fankhauser,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.

    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.

    For further information, please visit http://www.tik.ee.ethz.ch/~topsy

    This software is provided under the terms of the GNU General Public Licence.

    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/
/*
        
        File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Topsy/mips/SupportAsm.S,v $
        Author(s):             George Fankhauser
        Affiliation:           ETH Zuerich, TIK
        Version:               $Revision: 2922 $
        Creation Date:         
        Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
        
        
        $Log: SupportAsm.S,v $
        Revision 1.10  2000/11/15 10:20:36  ruf
        ia32Debug and Repository Reorganisation

        Revision 1.9  1999/12/13 21:48:39  ruf
        GNU General Public Licence Update

        Revision 1.8  1999/10/23 19:07:18  jeker
        added Makefile.SimOS.mips and did some code cleanup

        Revision 1.7  1999/10/23 14:19:49  jeker
        first cleanup for R4k support

        Revision 1.6  1999/10/22 19:22:44  gfa
        *** empty log message ***

        Revision 1.5  1998/04/08 15:09:29  gfa
        added getEPC, getBADVM

        Revision 1.4  1998/04/07 15:32:32  gfa
        getCP0reg

        Revision 1.3  1997/04/04 18:22:48  gfa
        replaced all k0/k1/at register references with t0/t1 in functions
        that do not run in exceptions (these registers are not saved on
        context switches and that's why they're called kernel scratch).

# Revision 1.2  1997/03/27  14:13:39  gfa
# *** empty log message ***
#
# Revision 1.1  1997/03/18  13:13:33  gfa
# Initial revision
#
*/

#include "asm.h"
#include "cpu.h"

        .text
        .align 2

        FRAME(testAndSet)

	.set noreorder
		
	mfc0	t0, C0_STATUS
	mtc0	zero, C0_STATUS
	nop			/* interrupts disabled */
	
	lw	t1, 0(a0)	/* load lockVariable contents */
	move	v0, zero	/* prepare return FALSE in load delay */
	bnez	t1, lockIsAlreadySet
	nop
	addiu	v0, 1		/* return TRUE (successful locking) */
	sw	v0, 0(a0)	/* write back locked value */
lockIsAlreadySet:
	mtc0	t0, C0_STATUS	/* re-enable interrupts */	
	
	.set reorder
	
        END(testAndSet)

	FRAME(getEPC)

	.set noreorder

	mfc0	v0, C0_EPC 
	nop

	.set reorder

	END(getEPC)

        FRAME(getBADVM)

        .set noreorder

        mfc0    v0, C0_BADVM
        nop

        .set reorder

	END(getBADVM)

        FRAME(getPRID)

        .set noreorder

        mfc0    v0, C0_PRID
        nop

        .set reorder

        END(getPRID)

