[C語言]映射函數模型實作-直接映射

範例輸入

記憶體:16MB
快取記憶體:64KB
快取區塊:4B
結果:0CE7




  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//string to HEX
//printf("%08x\n",foo("0xff"))
unsigned int foo(const char * s) {
 unsigned int result = 0;
 int c ;
 if ('0' == *s && 'x' == *(s+1)) { s+=2;
  while (*s) {
   result = result << 4;
   if (c=(*s-'0'),(c>=0 && c <=9)) result|=c;
   else if (c=(*s-'A'),(c>=0 && c <=5)) result|=(c+10);
   else if (c=(*s-'a'),(c>=0 && c <=5)) result|=(c+10);
   else break;
   ++s;
  }
 }
 return result;
}

//取得2的次方
int getPower(int value){
    int count=0;
    int chk=1;
    while(chk){
        value=value/2;
        if (value<2){
            chk=0;
        }
        count++;
    }
    return count;
}


//十進制轉二進制
char * toBin(int value){
    char *s = malloc(50 * sizeof(char));
    itoa(value, s, 2);
    return s;
}

//二進制轉十六進制
void binaryToHex(const char *inStr, char *outStr) {
    // outStr must be at least strlen(inStr)/4 + 1 bytes.
    static char hex[] = "0123456789ABCDEF";
    int len = strlen(inStr) / 4;
    int i = strlen(inStr) % 4;
    char current = 0;
    if(i) { // handle not multiple of 4
        while(i--) {
            current = (current << 1) + (*inStr - '0');
            inStr++;
        }
        *outStr = hex[current];
        ++outStr;
    }
    while(len--) {
        current = 0;
        for(i = 0; i < 4; ++i) {
            current = (current << 1) + (*inStr - '0');
            inStr++;
        }
        *outStr = hex[current];
        ++outStr;
    }
    *outStr = 0; // null byte
}


int main(int argc, char** argv) {
    //
 int ramSize_MB;
 int cacheMemSize_KB;
 int cacheBlockSize_B;

 //
    int memAddress;
    char memAddress_s[50];

 //參數輸入;
    printf("RAM size(MB):");
    scanf("%d", &ramSize_MB);

    printf("Cache mem size(KB):");
    scanf("%d", &cacheMemSize_KB);

    printf("Cache block size(Byte):");
    scanf("%d", &cacheBlockSize_B);

    printf("Input a mem address(HEX):\nExample:0x113609C\n");
    scanf("%s", memAddress_s);

    memAddress=foo(memAddress_s);



    //
    ramSize_MB=ramSize_MB*pow(2, 20);
    cacheMemSize_KB=cacheMemSize_KB*pow(2, 10);

    //
    printf("\n");
    printf("切分成(塊):%d\n",ramSize_MB/cacheMemSize_KB);

    //線路
    printf("每個區塊大小(KB):%d\n",cacheMemSize_KB/1024);

    //每個區塊含有(條)線路
    printf("每個區塊含有(條)線路:%d\n",cacheMemSize_KB/cacheBlockSize_B);
    //int totalPath=cacheMemSize_KB/cacheBlockSize_B;

    //記憶體位置長度
    printf("總長度(bits):%d\n",getPower(ramSize_MB));
    int memLength=getPower(ramSize_MB);

    //
    printf("線路需要?bits定址:%d\n",getPower(cacheMemSize_KB/cacheBlockSize_B));
    int pathNeedBits=getPower(cacheMemSize_KB/cacheBlockSize_B);

    //
    printf("字組需要?bits定址:%d\n",getPower(cacheBlockSize_B));
    int wordsNeedBits=getPower(cacheBlockSize_B);

    //標籤大小(剩下沒用到的定址空間)
    printf("標籤可用大小(bits):%d\n",memLength-pathNeedBits-wordsNeedBits);
    int tagSize=memLength-pathNeedBits-wordsNeedBits;

    //處理輸入的記憶體
    printf("\n");
    printf("MEM(HEX):0x%x\n",memAddress);
    printf("MEM(BIN):%s\n",toBin(memAddress));
    char *memAddress_bin=malloc(50 * sizeof(char));
    memAddress_bin=toBin(memAddress);

    //算陣列長度>取得位移
    int length=0;
    while(memAddress_bin[length]!=0){length++;} // array final char \0
    length++;
    int shifting=length % 4;
 //printf("%d\n",length % 4);


    printf("標籤:");
    int flag,flag2,flag3;
    for(flag=0;flag<tagSize-(shifting+1);flag++){
        printf("%c",memAddress_bin[flag]);
    }
    //printf("\n%d",flag);

    printf("線路:");
    char cachePathNum[50];
    int tmpCount=0;
    flag2=flag;
    for(flag=flag;flag<(pathNeedBits+flag2);flag++){
        printf("%c",memAddress_bin[flag]);
        cachePathNum[tmpCount]=memAddress_bin[flag];
        tmpCount++;
    }

    printf("字組:");
    flag3=flag;
     for(flag=flag;flag<(wordsNeedBits+flag3);flag++){
        printf("%c",memAddress_bin[flag]);
    }

    //結果
    binaryToHex(cachePathNum,cachePathNum);
    printf("\n快取線路:0x%s",cachePathNum);


 return 0;
}

留言

這個網誌中的熱門文章

[Arduino]電子秤平 重量感測條+HX711AD模組

cpe練習筆記 UVa401 Palindromes

cpe練習筆記 UVa10019 Funny Encryption Method