# TrueReality - TLB documentation                                       #
# Copyright (C) 1999 Niki W. Waibel                                     #
#                                                                       #
# This program is free software; you can redistribute it and/           #
# or modify it under the terms of the GNU General Public Li-            #
# cence as published by the Free Software Foundation; either            #
# version 2 of the Licence, or any later version.                       #
#                                                                       #
# This program is distributed in the hope that it will be use-          #
# ful, but WITHOUT ANY WARRANTY; without even the implied war-          #
# ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         #
# See the GNU General Public Licence for more details.                  #
#                                                                       #
# You should have received a copy of the GNU General Public             #
# Licence along with this program; if not, write to the Free            #
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,         #
# USA.                                                                  #
#                                                                       #
# Information about me (the author):                                    #
#   Niki W. Waibel, Reichenau 20, 6890 Lustenau, Austria - EUROPE       #
#   niki.waibel@gmx.net                                                 #


=========================================================================
This is some sort of the TLB (Translation Lookaside Buffer) ducumentation
=========================================================================


All of this stuff was figured out by disassembling/emulating various demos
with TrueReality and looking into the R4300 manual.
All things which I'm not sure about are marked with a '?'.
Gil Pedersen (MacOS port) was the first one that implemented this in TR.
The CPU commands were done by me long time ago.
I cleaned Gils stuff and reimplemented it.





Memory map:
        The following is wrong in most emus! It was wrong in my emulation
        for a long time.

        0x00000000 - 0x7fffffff: mapped memory
        0x80000000 - 0x9fffffff: physical memory (cachable)
        0xa0000000 - 0xbfffffff: physical memory (uncachable) (copy of the above!)
        0xc0000000 - 0xdfffffff: mapped memory
        0xe0000000 - 0xffffffff: mapped memory

        If the mem region is cachable or not is compleately unimportant for
        emulation!
        Normally the read and writes are just done in region 0x80000000 -
        0xbfffffff. If not then there is sthg wrong in your emulation.
        
        That means for example that RDRAM ranges from
        0x80000000 to 0x803fffff
        and a copy of that from
        0xa0000000 to 0xa03fffff.
        There is NO RDMEM from
        0x00000000 to 0x003fffff!!!

        The same mirroring is done with all registers which are mapped into
        the memory (there is no IO stuff like on the ix86).

        ? If there is a mem extension plugged in into the n64 then the RDMEM
        range is twice as large.

        If there is a read/write on mem (in the physical area!) which does not
        exist you can return zero.
        In reality sthg like electrical noise is returned (quor: thanks for
        this info!).

        There are some of such read/writes in the bootcode in most demos i
        tested. The addresses are:
        0xa3f04004, 0xa3f80004, 0xa3f80008, 0xa3f8000c and 0xa3f80014
        It is okay to return zero or any other number here - as i described
        above.

What CPU commands are important for TLB?
        TLBP
        TLBR
        TLBWI
        TLBWR
        ... look into the R4300 manual what they do.
        ... or into Source/N64/cpu.c :)

What CPU exceptions are important for TLB?
        TLB_MISS
        TLB_MODIFY
        TLB_INVALID

        ? It seems that the TLB miss exception is not executed.
        Strange.

When do i need TLB translation?
        If memory in the ranges
        0x00000000 - 0x7fffffff
        0xc0000000 - 0xdfffffff
        0xe0000000 - 0xffffffff
        is accessed.
        I hope that this just happens for data.
        It could happen for instructions too but that would not work.

How do i need to translate the memory address?
        Look into Source/N64/memory.c:
        static char *get_mapped(uWORD virtualaddr)
        static char *put_mapped(uWORD virtualaddr)
        ... i have not figured out how the ASID stuff works.
        But it does not seem to be important.

