Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. #define TLB_SIZE 4
  5. #define PAGE_TABLE_SIZE 10
  6. #define OFF_LEN 12
  7.  
  8. typedef uint32_t address_t ;
  9. typedef uint32_t index_t ;
  10. typedef uint32_t offset_t ;
  11.  
  12. struct tlb_slot_t {
  13. index_t virtual ; // Index for v i r t u al address
  14. index_t physical ; // Index for p hy s ic al address
  15. index_t count ; // Number o f times t h i s s l o t i s accessed
  16. };
  17.  
  18. struct tlb_slot_t tlb [TLB_SIZE] ;
  19.  
  20. struct tlb_slot_t page_table [PAGE_TABLE_SIZE] ;
  21.  
  22. /* Get index par t o f an address */
  23. index_t get_index ( address_t addr ) ;
  24.  
  25. /* Get o f f s e t par t o f an address */
  26. offset_t get_offset ( address_t addr ) ;
  27.  
  28. /* Get the l e a s t accessed s l o t from TLB */
  29. struct tlb_slot_t * get_least_accessed_slot () ;
  30.  
  31. /* Translate v i r t u al address to p hy s ic al address */
  32. int translate ( address_t virtual , address_t * physical ) {
  33. // TODO: Translate v i r t u al address to p hy s ic al address .
  34. // Return 1 i f the v i r t u al address i s val id , otherw ise , return 0
  35. // 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
  36. // to p hy s ic al address .
  37. return 0;
  38.  
  39. }
  40.  
  41. int main ( ) {
  42. /* I n i t page t a bl e */
  43. page_table [ 0 ] = ( struct tlb_slot_t ) {0x00001 , 0x52354 , 0};
  44. page_table [ 1 ] = ( struct tlb_slot_t ) {0x00002 , 0xafb29 , 0};
  45. page_table [ 2 ] = ( struct tlb_slot_t ) {0x00003 , 0x4b0dc , 0};
  46. page_table [ 3 ] = ( struct tlb_slot_t ) {0x00004 , 0x52ca0 , 0};
  47. page_table [ 4 ] = ( struct tlb_slot_t ) {0x00005 , 0xa7cbd , 0};
  48. page_table [ 5 ] = ( struct tlb_slot_t ) {0x17d42 , 0x338a3 , 0};
  49. page_table [ 6 ] = ( struct tlb_slot_t ) {0x1238f , 0x28471 , 0};
  50. page_table [ 7 ] = ( struct tlb_slot_t ) {0xda234 , 0x2341b , 0};
  51. page_table [ 8 ] = ( struct tlb_slot_t ) {0xf1234 , 0x1bca2 , 0};
  52. page_table [ 9 ] = ( struct tlb_slot_t ) {0x129af , 0x23133 , 0};
  53.  
  54. /* I n i t TLB */
  55. tlb [ 0 ] = page_table [ 0 ] ; tlb [ 1 ] = page_table [ 1 ] ;
  56. tlb [ 2 ] = page_table [ 2 ] ; tlb [ 3 ] = page_table [ 3 ] ;
  57.  
  58. /* I n i t t e s t s */
  59. int test [7] =
  60. {0x00003123 , 0x00001524 , 0x00002534 , 0x17d42e52 ,
  61. 0x121aabdd , 0x000012ac , 0x00004a71 };
  62. int i ;
  63. printf ("Page table\n" ) ;
  64. for ( i = 0; i < PAGE_TABLE_SIZE; i++) {
  65. printf ("%05x −−> %05x\n" ,
  66. page_table[i].virtual , page_table[i].physical ) ;
  67. }
  68.  
  69. /* Test */
  70. printf ("Access pages\n" ) ;
  71. for ( i = 0; i < 7; i++) {
  72. address_t addr ;
  73. if ( translate ( test[i] , &addr ) ) {
  74. printf ("%08x −−> %08x\n" , test[i] , addr ) ;
  75. }else{
  76. printf ("%08x −−> Illegaladdress\n" , test[ i ] ) ;
  77. }
  78. }
  79.  
  80. /* The TLB */
  81. printf ("TLB\n" ) ;
  82. for ( i = 0; i < TLB_SIZE; i++) {
  83. printf ("%d: %05x −−> %05x : %2d\n" ,
  84. i , tlb[i].virtual , tlb[i].physical , tlb[i].count ) ;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement