;
; group v, test 1
;
; priv mode trap.  We switch to user mode in this test, but should exit in supervisor mode.

m4_include(..\tmacros.h)

INIT_TEST(v,0x01)

; declare symbols here
SYM(trap_addr)
SYM(do_priv_mode)
SYM(resume_addr)
SYM(fail0)

SUBTEST(1)
    ld.16   a,do_priv_mode
    ld.16   b,v_priv
    st.16   0(b),a
    ld.16   a,0x7000
    copy    sp,a
    ld.16   a,0x8000
    add.16  a,a
    ld.16   a,0x1111
    copy    dp,a
    ld.16   b,0x2222
    ld.16   c,0x3333
    copy    a,msw
    or.16   a,0x10
; switch to user mode
    copy    msw,a
trap_addr:
; this should trap now
    copy    msw,a
    halt

fail0:
    FAIL

do_priv_mode:
  
    ld.16    a,0(sp)
    cmpb.ne.16	a,0x1111,fail0
    ld.16   a,2(sp)
    cmpb.ne.16	a,0x3333,fail0
    ld.16   a,4(sp)
    cmpb.ne.16	a,0x2222,fail0
    ld.16   a,8(sp)
    cmpb.ne.16	a,trap_addr,fail0
    ld.16   a,10(sp)
    and.16  a,0x000f
    cmpb.ne.16	a,11,fail0
    ld.16   a,12(sp)
    cmpb.ne.16	a,0x7000,fail0

; switch to back to priv mode
    ld.16   a,10(sp)
    and.16  a,0xffef
    st.16   10(sp),a

    ld.16   a,8(sp)
    add.16  a,(resume_addr-trap_addr)
    st.16   8(sp),a
    reti
    FAIL

resume_addr:

;clean up
    ld.16   a,_unhandled_trap
    ld.16   b,v_priv
    st.16   0(b),a

; now, make sure we're back in priv mode
    copy    a,msw
    copy    msw,a

;   Finally, when done branch to pass
    END_TEST
