Hi!
To be honest, the low level hardware detection is performed by the SDK i use: GBDK-2020 https://github.com/gbdk-2020/gbdk-2020
The test itself is made at startup: https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/libc/targets/gbz80/... (see __cpu variable)
Basically, the boot rom is slightly different and leaves the values of some register in different states at startup, and that's how we can detect which GB model we are running on (for DMG / Pocket / Color / Advance detection). For the detection of SGB, it's different: we ask to enable some SGB only features (such as mutliplayer support). If the machine replies a with specific value, then it's running on a SGB, else it does nothing meaning it's not running on a SGB but on another Game Boy model.