Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #define TLB_SIZE 4
- #define PAGE_TABLE_SIZE 10
- #define OFF_LEN 12
- typedef uint32_t address_t ;
- typedef uint32_t index_t ;
- typedef uint32_t offset_t ;
- struct tlb_slot_t {
- index_t virtual ; // Index for v i r t u al address
- index_t physical ; // Index for p hy s ic al address
- index_t count ; // Number o f times t h i s s l o t i s accessed
- };
- struct tlb_slot_t tlb [TLB_SIZE] ;
- struct tlb_slot_t page_table [PAGE_TABLE_SIZE] ;
- /* Get index par t o f an address */
- index_t get_index ( address_t addr ) ;
- /* Get o f f s e t par t o f an address */
- offset_t get_offset ( address_t addr ) ;
- /* Get the l e a s t accessed s l o t from TLB */
- struct tlb_slot_t * get_least_accessed_slot () ;
- /* Translate v i r t u al address to p hy s ic al address */
- int translate ( address_t virtual , address_t * physical ) {
- // TODO: Translate v i r t u al address to p hy s ic al address .
- // Return 1 i f the v i r t u al address i s val id , otherw ise , return 0
- // I f the v i r t u al address i s val id , wr i t ing i t s p hy s ic al counterpart
- // to p hy s ic al address .
- return 0;
- }
- int main ( ) {
- /* I n i t page t a bl e */
- page_table [ 0 ] = ( struct tlb_slot_t ) {0x00001 , 0x52354 , 0};
- page_table [ 1 ] = ( struct tlb_slot_t ) {0x00002 , 0xafb29 , 0};
- page_table [ 2 ] = ( struct tlb_slot_t ) {0x00003 , 0x4b0dc , 0};
- page_table [ 3 ] = ( struct tlb_slot_t ) {0x00004 , 0x52ca0 , 0};
- page_table [ 4 ] = ( struct tlb_slot_t ) {0x00005 , 0xa7cbd , 0};
- page_table [ 5 ] = ( struct tlb_slot_t ) {0x17d42 , 0x338a3 , 0};
- page_table [ 6 ] = ( struct tlb_slot_t ) {0x1238f , 0x28471 , 0};
- page_table [ 7 ] = ( struct tlb_slot_t ) {0xda234 , 0x2341b , 0};
- page_table [ 8 ] = ( struct tlb_slot_t ) {0xf1234 , 0x1bca2 , 0};
- page_table [ 9 ] = ( struct tlb_slot_t ) {0x129af , 0x23133 , 0};
- /* I n i t TLB */
- tlb [ 0 ] = page_table [ 0 ] ; tlb [ 1 ] = page_table [ 1 ] ;
- tlb [ 2 ] = page_table [ 2 ] ; tlb [ 3 ] = page_table [ 3 ] ;
- /* I n i t t e s t s */
- int test [7] =
- {0x00003123 , 0x00001524 , 0x00002534 , 0x17d42e52 ,
- 0x121aabdd , 0x000012ac , 0x00004a71 };
- int i ;
- printf ("Page table\n" ) ;
- for ( i = 0; i < PAGE_TABLE_SIZE; i++) {
- printf ("%05x −−> %05x\n" ,
- page_table[i].virtual , page_table[i].physical ) ;
- }
- /* Test */
- printf ("Access pages\n" ) ;
- for ( i = 0; i < 7; i++) {
- address_t addr ;
- if ( translate ( test[i] , &addr ) ) {
- printf ("%08x −−> %08x\n" , test[i] , addr ) ;
- }else{
- printf ("%08x −−> Illegaladdress\n" , test[ i ] ) ;
- }
- }
- /* The TLB */
- printf ("TLB\n" ) ;
- for ( i = 0; i < TLB_SIZE; i++) {
- printf ("%d: %05x −−> %05x : %2d\n" ,
- i , tlb[i].virtual , tlb[i].physical , tlb[i].count ) ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement