Skip to content

Commit

Permalink
CPUコアとデバイスの動作を分離、SLP/HLT が発行されたときはCPUを手放すようになった
Browse files Browse the repository at this point in the history
  • Loading branch information
autch committed Jun 11, 2018
1 parent 78643c6 commit c66937b
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 185 deletions.
2 changes: 0 additions & 2 deletions app.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <string.h>
#include <stdarg.h>

#include "sleep.h"

//#include "resource.h"

#define APPNAME "P/EMU"
Expand Down
53 changes: 37 additions & 16 deletions core.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,50 @@ core_init(PIEMU_CONTEXT *context)
{
memset(&context->core, 0, sizeof context->core);
PC = mem_readW(context, 0x0c00000);

context->core.cond_halt = SDL_CreateCond();
context->core.mut_halt = SDL_CreateMutex();
context->core.mut_core = SDL_CreateMutex();
}

void
core_work(PIEMU_CONTEXT *context)
{
INST inst;

if (context->core.in_halt) {
STD_NOP;
CLK += HALT_INT_CLK; //1 * NOP_CLK_MULTIPLY;
return;
}

inst.s = mem_readH(context, PC);
core_inst(context, inst);
}

void core_handle_hlt(PIEMU_CONTEXT* context)
{
if(context->core.in_halt == 0)
return;

SDL_LockMutex(context->core.mut_halt);
{
while(context->core.in_halt != 0) {
SDL_CondWait(context->core.cond_halt, context->core.mut_halt);
}
}
SDL_UnlockMutex(context->core.mut_halt);
}

unsigned
core_workex(PIEMU_CONTEXT *context, unsigned mils_org, unsigned nClocksDivBy1k)
{
INST inst;
unsigned insts = 0;
do {
inst.s = mem_readH(context, PC);
core_inst(context, inst);
// inst.s = mem_readH(context, PC); core_inst(context, inst);
// inst.s = mem_readH(context, PC); core_inst(context, inst);
// inst.s = mem_readH(context, PC); core_inst(context, inst);
core_work(context);
insts++;
if(context->core.in_halt)
break;
} while (!context->bEndFlag && (CLK - mils_org) < nClocksDivBy1k); /* 1ミリ秒分の処理 */
return insts;
}

void
core_trap(PIEMU_CONTEXT *context, int no, int level)
core_trap_from_core(PIEMU_CONTEXT *context, int no, int level)
{
c33word addr;

Expand All @@ -78,9 +87,6 @@ core_trap(PIEMU_CONTEXT *context, int no, int level)
if ((unsigned) level <= PSR.il) return;
}

if (context->core.in_halt)
context->core.in_halt = 0;

addr = mem_readW(context, pTTBR_REG + no * 4); /* ※要検討:トラップテーブル直読みしてます */
SP -= 4;
mem_writeW(context, SP, PC); /* 要注意!PC+2じゃないよ! */
Expand All @@ -95,6 +101,21 @@ core_trap(PIEMU_CONTEXT *context, int no, int level)
}
}

void
core_trap_from_devices(PIEMU_CONTEXT *context, int no, int level)
{
SDL_LockMutex(context->core.mut_core);
core_trap_from_core(context, no, level);
SDL_UnlockMutex(context->core.mut_core);

SDL_LockMutex(context->core.mut_halt);
{
context->core.in_halt = 0;
SDL_CondBroadcast(context->core.cond_halt);
}
SDL_UnlockMutex(context->core.mut_halt);
}

#if 1
#define MASK(op, shr, and) ((inst.s >> (16 - (shr + and))) & ((1 << and) - 1))
#else
Expand Down
18 changes: 8 additions & 10 deletions core/class0.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@
void exec_nop(PIEMU_CONTEXT *context, CLASS_0A inst)
{
NO_EXT NO_DELAY
STD_NOP;
PC += 2;
CLK += 1 * NOP_CLK_MULTIPLY;
CLK += 1;
}

// 呼ばれてないらしい?
void exec_slp(PIEMU_CONTEXT *context, CLASS_0A inst)
{
NO_EXT NO_DELAY
STD_NOP;

/* ※TODO: */
context->core.in_halt = 1;
PC += 2;
CLK += 1 * NOP_CLK_MULTIPLY;
CLK += 1;

context->core.in_halt = 1;
}

void exec_halt(PIEMU_CONTEXT *context, CLASS_0A inst)
{
NO_EXT NO_DELAY
STD_NOP;

/* ※TODO: */
/*context->core.in_halt = 1;*/
PC += 2;
CLK += 1 * HALT_CLK_MULTIPLY;
CLK += 1;

context->core.in_halt = 1;
}

void exec_pushn_rs(PIEMU_CONTEXT *context, CLASS_0A inst)
Expand Down Expand Up @@ -78,8 +77,7 @@ void exec_int_imm2(PIEMU_CONTEXT *context, CLASS_0A inst)
if (inst.imm2_rd_rs < 0 || inst.imm2_rd_rs > 3) DIE();
PC += 2;
CLK += 10;
core_trap(context, TRAP_SOFTINT0 + inst.imm2_rd_rs, 0);

core_trap_from_core(context, TRAP_SOFTINT0 + inst.imm2_rd_rs, 0);
}

void exec_reti(PIEMU_CONTEXT *context, CLASS_0A inst)
Expand Down
13 changes: 0 additions & 13 deletions core/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@
#define HALT_CLK_MULTIPLY 240
#define HALT_INT_CLK 24000

#if 1
#define STD_NOP { \
context->core.nop_count++; \
context->core.nop_count &= (1 << NOP_WAIT) - 1; \
if(!context->core.nop_count) \
{ \
SDL_Delay(1); \
} \
}
#else // 0
#define STD_NOP OS_YIELD() //__asm { nop }
#endif

/* 関数の先頭で使えるように、ダミー変数を初期化します。 */

// C33 には無効命令例外がないので、不正なオペコードを検知したとしてもできることはない。
Expand Down
Loading

0 comments on commit c66937b

Please sign in to comment.