#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum
{
InstrNop,
InstrHalt,
InstrSetVarNum,
InstrJumpVarZero,
InstrJumpVarNonzero,
InstrJump,
InstrIncVar,
InstrDecVar,
InstrXorVarNum,
} tInstr;
int ExecuteInstruction(unsigned* Vars, const unsigned* Program, unsigned* Position)
{
switch (Program[*Position])
{
default:
case InstrHalt:
return 0;
case InstrNop:
(*Position)++;
break;
case InstrSetVarNum:
Vars[Program[*Position + 1]] = Program[*Position + 2];
(*Position) += 3;
break;
case InstrXorVarNum:
Vars[Program[*Position + 1]] ^= Program[*Position + 2];
(*Position) += 3;
break;
case InstrJumpVarZero:
if (Vars[Program[*Position + 1]] == 0)
(*Position) = Program[*Position + 2];
else
(*Position) += 3;
break;
case InstrJumpVarNonzero:
if (Vars[Program[*Position + 1]] != 0)
(*Position) = Program[*Position + 2];
else
(*Position) += 3;
break;
case InstrJump:
(*Position) = Program[*Position + 1];
break;
case InstrIncVar:
Vars[Program[*Position + 1]]++;
(*Position) += 2;
break;
case InstrDecVar:
Vars[Program[*Position + 1]]--;
(*Position) += 2;
break;
}
return 1;
}
typedef enum
{
VarGlobal,
VarCnt0,
VarCnt1,
VarFlag0,
VarFlag1,
VarTurn,
VarIdxMax
} tVarIdx;
unsigned Vars[VarIdxMax];
#define USE_PETERSON 0
#define SWAP_CHECKS 0
const unsigned Program0[] =
{
// cnt0 = 1000;
InstrSetVarNum, VarCnt0, 1000,
// 3:
#if USE_PETERSON
// flag[0] = 1;
InstrSetVarNum, VarFlag0, 1,
// turn = 1;
InstrSetVarNum, VarTurn, 1,
// 9:
// while (flag[1] == 1 && turn == 1) {}
#if SWAP_CHECKS
InstrJumpVarZero, VarTurn, 17,
InstrJumpVarZero, VarFlag1, 17,
#else
InstrJumpVarZero, VarFlag1, 17,
InstrJumpVarZero, VarTurn, 17,
#endif
InstrJump, 9,
// 17:
#endif
// Critical section starts
// global ^= 0x5555;
// global ^= 0x5555;
// global++;
InstrXorVarNum, VarGlobal, 0x5555,
InstrXorVarNum, VarGlobal, 0x5555,
InstrIncVar, VarGlobal,
// Critical section ends
#if USE_PETERSON
// flag[0] = 0;
InstrSetVarNum, VarFlag0, 0,
#endif
// cnt0--;
InstrDecVar, VarCnt0,
// if (cnt0 == 0) goto 3;
InstrJumpVarNonzero, VarCnt0, 3,
// end
InstrHalt
};
const unsigned Program1[] =
{
// cnt1 = 1000;
InstrSetVarNum, VarCnt1, 1000,
// 3:
#if USE_PETERSON
// flag[1] = 1;
InstrSetVarNum, VarFlag1, 1,
// turn = 0;
InstrSetVarNum, VarTurn, 0,
// 9:
// while (flag[0] == 1 && turn == 0) {}
#if SWAP_CHECKS
InstrJumpVarNonzero, VarTurn, 17,
InstrJumpVarZero, VarFlag0, 17,
#else
InstrJumpVarZero, VarFlag0, 17,
InstrJumpVarNonzero, VarTurn, 17,
#endif
InstrJump, 9,
// 17:
#endif
// Critical section starts
// global ^= 0xAAAA;
// global ^= 0xAAAA;
// global++;
InstrXorVarNum, VarGlobal, 0xAAAA,
InstrXorVarNum, VarGlobal, 0xAAAA,
InstrIncVar, VarGlobal,
// Critical section ends
#if USE_PETERSON
// flag[1] = 0;
InstrSetVarNum, VarFlag1, 0,
#endif
// cnt1--;
InstrDecVar, VarCnt1,
// if (cnt1 == 0) goto 3;
InstrJumpVarNonzero, VarCnt1, 3,
// end
InstrHalt
};
void Simulate(void)
{
unsigned pos0 = 0, pos1 = 0;
while (Program0[pos0] != InstrHalt ||
Program1[pos1] != InstrHalt)
{
int cnt;
while (cnt--)
if (!ExecuteInstruction(Vars, Program0, &pos0))
break;
while (cnt--)
if (!ExecuteInstruction(Vars, Program1, &pos1))
break;
}
}
int main(void)
{
Simulate();
printf("VarGlobal = %u\n", Vars
[VarGlobal
]); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCnR5cGVkZWYgZW51bQp7CiAgSW5zdHJOb3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