;
; group x, test 2
;
; page not writeable trap

m4_include(..\tmacros.h)

INIT_TEST(x,0x02)

; declare symbols here
SYM(next0)
SYM(next1)
SYM(handle_nw)
SYM(pre_trap)
SYM(post_trap)


SUBTEST(1)

;   set up stack
    ld.16   a,0x7000
    copy    sp,a

;   set things up
    call    setup_page_table

;   Install not present trap handler
    ld.16   b,v_nw
    ld.16   a,handle_nw
    st.16   0(b),a

;   make code page 10 present, but not writeable
    ld.16   a,0x800b
    ld.16   b,0x5000
    wdpte   a,(b)

;   turn paging on
    call    paging_on

; Use test location 0x5000, stuff 0x1234 in data, 0x4321 in code
    ld.16   b,0x5000
    ld.16   a,0x1234
;   Read should be okay, this one shouldn't trap
    ld.16   b,0(b)
    cmpb.eq.16	a,0x1234,next0
    FAIL
next0:

;   Write should trap
    ld.16   b,0x5000
    ld.16   a,0x1234
pre_trap:
    st.16   0(b),a
post_trap:
    cmpb.eq.16	a,0x4321,next1
    FAIL


handle_nw:
    ld.16   a,8(sp)
    add.16  a,(post_trap-pre_trap)
    st.16   8(sp),a
    ld.16   a,0x4321
    st.16   6(sp),a
    reti

next1:
;   Clean up
    call    paging_off
    call    setup_page_table
;   Remove not present trap handler
    ld.16   a,_unhandled_trap
    ld.16   b,v_nw
    st.16   0(b),a

;   Finally, when done branch to pass
    END_TEST


